I used to rotate an image this way:
self.image.transform = CGAffineTransformRotate(self.image.transform,
CGFloat(M_PI))
But this code doesn't work for a button:
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = CGFloat(M_PI)
But I don't know how to assign that rotation to the button. How can I do it?
You have created a CABasicAnimation
. You should now add it to a CALayer
. Try using this code:
button.layer.addAnimation(rotateAnimation, forKey: "myAnimationKey");
Set a duration this way:
let duration = 1.0
rotateAnimation.duration = duration
A UIButton
has a layer Property.
Just add your animation to the layer of your button.
//If you want a duration
rotateAnimation.duration = yourDuration
//You can set a key, but must not.
button.layer.addAnimation(rotateAnimation, forKey: nil)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.