我想旋转UIImage
,我已经使用下面的代码成功做到了这一点,但是当我再次按下rotate
按钮时,图像不再旋转,有人可以解释一下为什么吗?
@IBAction func rotate(sender: UIButton) {
UIView.animateWithDuration(0.2, animations: {
self.shape.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_4) * 2)
})
}
最佳答案
您正在将形状 ImageView 的转换更改为新的固定值。如果您再次点击它,变换已经具有该值。您再次将变换设置为相同的值,这不会改变任何内容。
您需要定义一个实例变量来跟踪旋转。
var rotation: CGFloat = 0
@IBAction func rotate(sender: UIButton)
{
UIView.animateWithDuration(0.2, animations:
{
self.rotation += CGFloat(M_PI_4) * 2 //changed based on Daniel Storm's comment
self.shape.transform = CGAffineTransformMakeRotation(rotation)
})
}
这样,每次点击按钮时,您都会将旋转变量从之前的值更改为新值,并旋转到新角度。
关于ios - 点击重复动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30548923/