旋转UIButton 360度

Nad*_*aie 17 uibutton uiview ios swift

我一直在尝试UIButton使用以下代码运行旋转360度的动画:

UIView.animateWithDuration(3.0, animations: {
  self.vineTimeCapButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI*2))
  self.instagramTimeCapButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI*2))
})
Run Code Online (Sandbox Code Playgroud)

但是,它不会旋转360度,因为UIButton它已经在该位置.

如何旋转我的UIButton 360度?

小智 35

您可以使用技巧:首先以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)
Run Code Online (Sandbox Code Playgroud)

斯威夫特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)
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助

  • 你是惊人的!! 仅建议:为了使其平滑360度旋转,第二个"animateWithDuration"的延迟需要是动画持续时间的一半.非常感谢!! (4认同)
  • 零延迟应该没问题! (2认同)

Kqt*_*qtr 8

作为讨论在这里,你还可以使用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")
Run Code Online (Sandbox Code Playgroud)

您需要导入QuartzCore:

import QuartzCore
Run Code Online (Sandbox Code Playgroud)

ViewController需要遵守CAAnimationDelegate:

class ViewController: UIViewController, CAAnimationDelegate {

}
Run Code Online (Sandbox Code Playgroud)


小智 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)
Run Code Online (Sandbox Code Playgroud)


Kru*_*nal 7

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)))
       }    
}
Run Code Online (Sandbox Code Playgroud)


bud*_*ino 6

允许您无限次旋转相同视图的Swift 4版本:

UIView.animate(withDuration: 0.5) {
  self.yourButton.transform = self.yourButton.transform.rotated(by: CGFloat.pi)
}
Run Code Online (Sandbox Code Playgroud)

如果要旋转 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)
}
Run Code Online (Sandbox Code Playgroud)


Abh*_*rya 5

您还可以尝试实现一个扩展,如果您需要多次执行此操作,它将简化事情

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()
    }
}
Run Code Online (Sandbox Code Playgroud)