【问题标题】:How do I rotate an UIButton in Swift?如何在 Swift 中旋转 UIButton?
【发布时间】:2015-01-26 16:54:05
【问题描述】:
我以前是这样旋转图像的:
self.image.transform = CGAffineTransformRotate(self.image.transform,
CGFloat(M_PI))
但此代码不适用于按钮:
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = CGFloat(M_PI)
但我不知道如何将该旋转分配给按钮。我该怎么做?
【问题讨论】:
标签:
ios
swift
uibutton
rotation
【解决方案1】:
您创建了一个CABasicAnimation。您现在应该将其添加到 CALayer。
尝试使用此代码:
button.layer.addAnimation(rotateAnimation, forKey: "myAnimationKey");
以这种方式设置持续时间:
let duration = 1.0
rotateAnimation.duration = duration
UIButton 有一个 layer 属性。
【解决方案2】:
只需将动画添加到按钮的图层即可。
//If you want a duration
rotateAnimation.duration = yourDuration
//You can set a key, but must not.
button.layer.addAnimation(rotateAnimation, forKey: nil)