Created
August 30, 2022 20:41
-
-
Save Anik0808/2b276f771e8f6133754881ee12c12c6e to your computer and use it in GitHub Desktop.
add multiple animation to a CALayer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 | |
positionAnimation.fillMode = .forwards | |
positionAnimation.isRemovedOnCompletion = false | |
//create a rotation animatio | |
let rotateAnimation = CABasicAnimation(keyPath: #keyPath(CALayer.transform)) | |
rotateAnimation.valueFunction = CAValueFunction(name: .rotateZ) | |
rotateAnimation.duration = 2 | |
rotateAnimation.toValue = 90.degreesToRadians // provide radian value | |
//add the animations to the layer | |
layerToAnimate.add(positionAnimation, forKey: "positionAnimation") | |
layerToAnimate.add(rotateAnimation, forKey: "rotateAnimation") | |
//add the layer to the parent view | |
view.layer.addSublayer(layerToAnimate) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment