[英]How do I rotate an UIButton in Swift?
我以這種方式旋轉圖像:
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)
但我不知道如何將旋轉分配給按鈕。 我該怎么做?
您已經創建了CABasicAnimation
。 您現在應該將它添加到CALayer
。 嘗試使用此代碼:
button.layer.addAnimation(rotateAnimation, forKey: "myAnimationKey");
以這種方式設置持續時間:
let duration = 1.0
rotateAnimation.duration = duration
UIButton
有一個圖層屬性。
只需將動畫添加到按鈕圖層即可。
//If you want a duration
rotateAnimation.duration = yourDuration
//You can set a key, but must not.
button.layer.addAnimation(rotateAnimation, forKey: nil)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.