Skip to content

Instantly share code, notes, and snippets.

@Anik0808
Anik0808 / SpringAnimation.swift
Last active August 30, 2022 19:44
creates a spring animation that changes x position of a CALayer
//create a layer
let layerToAnimate = CALayer()
layerToAnimate.backgroundColor = UIColor.red.cgColor
layerToAnimate.frame = CGRect(x: 100, y: 200, width: 100, height: 100)
//create a "x" position changing animation
let positionAnimation = CASpringAnimation(keyPath: "position.x")
positionAnimation.damping = 6 //How much the spring motion will be damped
positionAnimation.toValue = 300 // In the end layer center will be in this position
positionAnimation.duration = 1 // Time needed to complete the animation
@Anik0808
Anik0808 / keyframeAnimation.swift
Created August 30, 2022 20:06
creates a simple keyframe animation for scaling a CALayer
//create a layer
let layerToAnimate = CALayer()
layerToAnimate.backgroundColor = UIColor.red.cgColor
layerToAnimate.frame = CGRect(x: 100, y: 200, width: 30, height: 30)
//create a scaling animation
let scaleAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
scaleAnimation.duration = 2 //Total animation duration
scaleAnimation.keyTimes = [0, 0.2, 0.5, 1.0] //keytimes within the animation duration[0.0-1.0]
scaleAnimation.values = [0, 2, 1, 3] //scale value of the keyTimes provided to the animation
@Anik0808
Anik0808 / multipleAnimation.swift
Created August 30, 2022 20:41
add multiple animation to a CALayer
//create a layer
let layerToAnimate = CALayer()
layerToAnimate.backgroundColor = UIColor.red.cgColor
layerToAnimate.frame = CGRect(x: 100, y: 200, width: 100, height: 100)
//create a "x" position changing animation
let positionAnimation = CABasicAnimation(keyPath: "position.x")
positionAnimation.toValue = 300 // In the end layer center will be in this position
positionAnimation.duration = 2 // Time needed to complete the animation
@Anik0808
Anik0808 / WindowBorder.swift
Created September 3, 2022 18:48
create a CALayer with border image
//create a layer
let windowBorderLayer = CALayer()
//give it a frame
windowBorderLayer.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 200, height: 200))
//set a border
windowBorderLayer.borderColor = UIColor(patternImage: UIImage(named: "windowImage")!).cgColor
windowBorderLayer.borderWidth = 20
//add to a view
animationView.layer.addSublayer(windowBorderLayer)
@Anik0808
Anik0808 / windowShadeLeft.swift
Last active September 3, 2022 19:21
create a window shade layer
//create a layer
let windowShadeLeft = CALayer()
//set the anchor point
windowShadeLeft.anchorPoint = CGPoint(x: 0, y: 0)
//give it a frame
windowShadeLeft.frame = CGRect(origin: CGPoint(x: 20, y: 20), size: CGSize(width: (200-40)/2, height: 200-40))
//set a image for the layer
windowShadeLeft.contents = UIImage(named: "windowImage")?.cgImage
windowShadeLeft.contentsGravity = .resizeAspectFill
//mask to bounds, so that images doen't show outside the layer
@Anik0808
Anik0808 / windowShadeRight.swift
Created September 3, 2022 19:23
Create a calayer
//create a layer
let windowShadeRight = CALayer()
//set it's anchor point
windowShadeRight.anchorPoint = CGPoint(x: 1, y: 0)
//set it's frame
windowShadeRight.frame = CGRect(origin: CGPoint(x: 100, y: 20), size: CGSize(width: (200-40)/2, height: 200-40))
//masks to bounds, so that images doen't show outside the layer
windowShadeRight.masksToBounds = true
//set a image for the layer
windowShadeRight.contents = UIImage(named: "windowImage")?.cgImage
@Anik0808
Anik0808 / anchorPoint.swift
Created September 3, 2022 20:07
Changed anchorPoint animation example
let layer = CALayer()
layer.backgroundColor = UIColor.red.cgColor
layer.anchorPoint = CGPoint(x: 0.0, y: 0.0)
layer.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
animationView.layer.addSublayer(layer)
let animation = CABasicAnimation(keyPath: "transform")
animation.duration = 2
animation.toValue = CATransform3DRotate(CATransform3DIdentity, (25.0 * .pi/180), 0, 0, 1)
animation.fillMode = .forwards
@Anik0808
Anik0808 / defaultAnchorPoint.swift
Created September 3, 2022 20:11
default anchorPoint animation
let layer = CALayer()
layer.backgroundColor = UIColor.red.cgColor
layer.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
animationView.layer.addSublayer(layer)
let animation = CABasicAnimation(keyPath: "transform")
animation.duration = 2
animation.toValue = CATransform3DRotate(CATransform3DIdentity, (25.0 * .pi/180), 0, 0, 1)
animation.fillMode = .forwards
animation.isRemovedOnCompletion = false
let layer = CALayer()
layer.backgroundColor = UIColor.red.cgColor
layer.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
animationView.layer.addSublayer(layer)
var transform = CATransform3DIdentity
transform = CATransform3DRotate(transform, (180 * .pi / 180), 0, 1, 0)
let animation = CABasicAnimation(keyPath: "transform")
animation.duration = 2
let layer = CALayer()
layer.backgroundColor = UIColor.red.cgColor
layer.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
animationView.layer.addSublayer(layer)
var transform = CATransform3DIdentity
transform.m34 = -1.0/200
transform = CATransform3DRotate(transform, 180.degreesToRadians, 0, 1, 0)
let animation = CABasicAnimation(keyPath: "transform")