Last active
January 16, 2017 16:04
-
-
Save mkj-is/d5f83262db60dbf8c4ef10493ad594bb to your computer and use it in GitHub Desktop.
Adding linear animation to shape layer
This file contains hidden or 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 shape layer and add the path to it | |
let layer = CAShapeLayer() | |
layer.path = path.cgPath | |
// Set up the appearance of the shape layer | |
layer.strokeEnd = 0 | |
layer.lineWidth = 1 | |
layer.strokeColor = UIColor.black.cgColor | |
layer.fillColor = UIColor.clear.cgColor | |
// Create view and set its appearance | |
let view = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 150)) | |
view.backgroundColor = .white | |
// Add the shape layer to view | |
view.layer.addSublayer(layer) | |
// Create the animation for the shape view | |
let animation = CABasicAnimation(keyPath: "strokeEnd") | |
animation.toValue = 1 | |
animation.duration = 2 // seconds | |
animation.autoreverses = true | |
animation.repeatCount = .infinity | |
// And finally add the linear animation to the shape! | |
layer.add(animation, forKey: "line") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://blog.thefuntasty.com/swift-stroke-animations-e1b864917255