Thursday, February 21, 2019

Swift spin a view

We want to spin a view like button, image, use this code.
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotationAnimation.fromValue = 0.0
rotationAnimation.toValue = Float(M_PI * 2.0)
rotationAnimation.duration = 1
rotationAnimation.repeatCount = Float.infinity
image.layer.addAnimation(rotationAnimation, forKey: nil)
Change duration value to spin faster or slower.
Change repeatCount to a number for certain spin time.
rotationAnimation.repeatCount = 2
Want it swing like a teeter, change code to.
rotationAnimation.fromValue = -1.0
rotationAnimation.toValue = Float(1)
rotationAnimation.duration = 1
rotationAnimation.repeatCount = Float.infinity
rotationAnimation.autoreverses=true
To speed up, add this line.

rotationAnimation.speed=3.0

No comments:

Post a Comment