如何在无限循环中使用缩放旋转imageView

时间:2018-01-26 10:46:42

标签: uiimageview swift4 cgaffinetransform

我试图在无限循环中使用缩放旋转imageView。 例如。 ImageView将在无限循环中旋转,并且在循环中也将从1.0扩展到0.5,然后从0.5扩展到1.0。

但是我的下面的代码无法实现它

func rotateViewWith(duration: Double = 1.0) {

        UIView.animate(withDuration: duration, delay: 0.0, options: [.curveLinear], animations: {
            self.transform = self.transform.rotated(by: CGFloat.pi)
            self.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
        }) { finished in
            UIView.animate(withDuration: duration, delay: 0.0, options: [.curveLinear], animations: {
                self.transform = self.transform.rotated(by: CGFloat.pi)
                self.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
            }) { finished in
                self.rotateViewWith(duration: duration)
            }
        }
    }

请帮忙怎么做。以及如何在任何按钮点击时重置它。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果您使用关键帧,则可以执行重复动画。这种形式的东西

func rotateViewWith(duration: Double = 1.0) {
    UIView.animateKeyframes(withDuration:duration, delay:0.0, options:.`repeat`, animations:{
        UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.0, animations: { 
            self.transform = self.transform.rotated(by: 0.0)
            self.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
        })
        UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.5, animations: { 
            self.transform = self.transform.rotated(by: CGFloat.pi)
            self.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
        })
        UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.5, animations: { 
            self.transform = self.transform.rotated(by: CGFloat.pi)
            self.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
        })
    })
}

你可以通过调用

来阻止它
self.layer.removeAllAnimations()