我一直试图运行一个使用以下代码旋转我的UIButton 360度的动画:
UIView.animateWithDuration(3.0, animations: {
self.vineTimeCapButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI*2))
self.instagramTimeCapButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI*2))
})然而,它不会旋转360度,因为UIButton已经在那个位置。
如何旋转我的UIButton 360度?
发布于 2015-12-05 07:53:56
你可以用一个技巧:先开始180度旋转,然后360度旋转。使用2个延迟动画。尝尝这个。
UIView.animate(withDuration: 0.5) {
self.view.transform = CGAffineTransform(rotationAngle: .pi)
}
UIView.animate(
withDuration: 0.5,
delay: 0.45,
options: UIView.AnimationOptions.curveEaseIn
) {
self.view.transform = CGAffineTransform(rotationAngle: 2 * .pi)
}发布于 2017-07-01 15:00:49
正如讨论过的here一样,您也可以使用CAAnimation。此代码适用于一个完整的360轮:
Swift 3
let fullRotation = CABasicAnimation(keyPath: "transform.rotation")
fullRotation.delegate = self
fullRotation.fromValue = NSNumber(floatLiteral: 0)
fullRotation.toValue = NSNumber(floatLiteral: Double(CGFloat.pi * 2))
fullRotation.duration = 0.5
fullRotation.repeatCount = 1
button.layer.add(fullRotation, forKey: "360")您需要导入QuartzCore
import QuartzCore而且您的ViewController需要符合CAAnimationDelegate
class ViewController: UIViewController, CAAnimationDelegate {
}发布于 2017-05-02 13:31:39
Swift 4:动画嵌套闭包优于动画延迟块。
UIView.animate(withDuration: 0.5, animations: {
button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi)))
}) { (isAnimationComplete) in
// Nested Block
UIView.animate(withDuration: 0.5) {
button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi * 2)))
}
}https://stackoverflow.com/questions/34102331
复制相似问题