我一直在尝试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度?
您可以使用技巧:首先以180度旋转然后以360度旋转.使用2个延迟动画.试试这个.
UIView.animateWithDuration(0.5) { () -> Void in button.transform = CGAffineTransformMakeRotation(CGFloat(M_PI)) } UIView.animateWithDuration(0.5, delay: 0.45, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in button.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 2)) }, completion: nil)
斯威夫特4
UIView.animate(withDuration: 0.5) { () -> Void in self.settingsButton.transform = CGAffineTransform(rotationAngle: CGFloat.pi) } UIView.animate(withDuration: 0.5, delay: 0.45, options: UIViewAnimationOptions.curveEaseIn, animations: { () -> Void in self.settingsButton.transform = CGAffineTransform(rotationAngle: CGFloat.pi * 2.0) }, completion: nil)
希望这可以帮助
作为讨论在这里,你还可以使用CAAnimation
.此代码适用于一个完整的360度旋转:
斯威夫特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 { }
在Swift 3中:
UIView.animate(withDuration:0.5, animations: { () -> Void in button.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI)) }) UIView.animate(withDuration: 0.5, delay: 0.45, options: .curveEaseIn, animations: { () -> Void in button.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI * 2)) }, completion: nil)
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))) } }