Skip to content

Instantly share code, notes, and snippets.

@mkj-is
Last active January 16, 2017 16:04
Show Gist options
  • Save mkj-is/d5f83262db60dbf8c4ef10493ad594bb to your computer and use it in GitHub Desktop.
Save mkj-is/d5f83262db60dbf8c4ef10493ad594bb to your computer and use it in GitHub Desktop.
Adding linear animation to shape layer
// 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")
@mkj-is
Copy link
Author

mkj-is commented Jan 16, 2017

@mkj-is
Copy link
Author

mkj-is commented Jan 16, 2017

swift

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment