我有一个汽车图像,我只想在按下图像时使后轮旋转。是否可以选择图像的一部分进行动画处理?我当时想上传2张图片(汽车和车轮),因为汽车还不需要移动。我希望有一种更清洁的方法来做到这一点。 任何建议将不胜感激!
答案 0 :(得分:2)
添加以下扩展名:(如果您不知道-> What is Extension? How to Add Extension?,请单击问题超链接。)
extension UIView {
func rotate360Degrees(duration: CFTimeInterval = 2) {
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = CGFloat(Double.pi * 2)
rotateAnimation.isRemovedOnCompletion = false
rotateAnimation.duration = duration
rotateAnimation.repeatCount=Float.infinity
self.layer.add(rotateAnimation, forKey: nil)
}
}
用法:
override func viewDidLoad() {
super.viewDidLoad()
carFirstWheelImage.rotate360Degrees()
carSecondWheelImage.rotate360Degrees() // you can change the time interval to change speed
}
建议: 您还可以通过更改多个图像来更改背景。这将使您的汽车具有逼真的动画外观。那会让你的故事更精彩!