我一直试图使用此代码运行一个旋转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度?
答案 0 :(得分:33)
您可以使用技巧:首先以180度旋转然后以360度旋转。使用2个延迟动画。 试试这个。
namespace :admin do
resources :stores
root 'stores#index'
end
Swift 4
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)
希望这有帮助
答案 1 :(得分:8)
正如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 {
}
答案 2 :(得分:7)
在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)
答案 3 :(得分:5)
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)))
}
}
答案 4 :(得分:4)
您可以使用基于CABasicAnimation
( Swift 4.x )的小扩展程序:
extension UIButton {
func rotate360Degrees(duration: CFTimeInterval = 1.0, completionDelegate: AnyObject? = nil) {
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = CGFloat(.pi * 2.0)
rotateAnimation.duration = duration
if let delegate: AnyObject = completionDelegate {
rotateAnimation.delegate = delegate as? CAAnimationDelegate
}
self.layer.add(rotateAnimation, forKey: nil)
}
}
<强>用法强>:
例如,我们可以开始制作一个简单的按钮:
let button = UIButton()
button.frame = CGRect(x: self.view.frame.size.width/2, y: 150, width: 50, height: 50)
button.backgroundColor = UIColor.red
button.setTitle("Name your Button ", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)
然后我们可以构建它的选择器:
@objc func buttonAction(sender: UIButton!) {
print("Button tapped")
sender.rotate360Degrees()
}
答案 5 :(得分:4)
Swift 4 版本,允许您无限次旋转相同的视图:
UIView.animate(withDuration: 0.5) {
self.yourButton.transform = self.yourButton.transform.rotated(by: CGFloat.pi)
}
如果你想旋转360°:
UIView.animate(withDuration: 0.5) {
self.yourButton.transform = self.yourButton.transform.rotated(by: CGFloat.pi)
self.yourButton.transform = self.yourButton.transform.rotated(by: CGFloat.pi)
}
答案 6 :(得分:0)
您还可以尝试实现扩展,如果需要多次扩展可以简化操作
extension UIView {
func rotate360Degrees(duration: CFTimeInterval = 1, repeatCount: Float = .infinity) {
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = CGFloat(Double.pi * 2)
rotateAnimation.isRemovedOnCompletion = false
rotateAnimation.duration = duration
rotateAnimation.repeatCount = repeatCount
layer.add(rotateAnimation, forKey: nil)
}
// Call this if using infinity animation
func stopRotation () {
layer.removeAllAnimations()
}
}
答案 7 :(得分:-1)
快捷键5 。试试这个,对我有用
UIView.animate(withDuration: 3) {
self.yourButton.transform = CGAffineTransform(rotationAngle: .pi)
self.yourButton.transform = CGAffineTransform(rotationAngle: .pi * 2)
}