当前位置:  开发笔记 > IOS > 正文

旋转UIButton 360度

如何解决《旋转UIButton360度》经验,为你挑选了4个好方法。

我一直在尝试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度?



1> 小智..:

您可以使用技巧:首先以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)

希望这可以帮助


你是惊人的!! 仅建议:为了使其平滑360度旋转,第二个"animateWithDuration"的延迟需要是动画持续时间的一半.非常感谢!!

2> Kqtr..:

作为讨论在这里,你还可以使用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 {

}



3> 小智..:

在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)



4> Krunal..:

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)))
       }    
}

推荐阅读
php
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有