【问题标题】:Reverse the rotation of UIImageView using CAKeyframeAnimation使用 CAKeyframeAnimation 反转 UIImageView 的旋转
【发布时间】:2019-09-13 01:30:17
【问题描述】:

我正在使用以下代码无限旋转图像,旋转是顺时针方向,但我还需要它每旋转 1-2 圈将其反转为逆时针方向,然后再返回顺时针方向,如何让它工作?

    /// Image Rotation - CAKeyframeAnimation
func rotate(imageView: UIImageView, rotationSpeed: Double) {

    let animation = CAKeyframeAnimation(keyPath: "transform.rotation.z")

    animation.duration = rotationSpeed
    animation.fillMode = CAMediaTimingFillMode.forwards
    animation.repeatCount = .infinity
    animation.values = [0, Double.pi/2, Double.pi, Double.pi*3/2, Double.pi*2]

    /// Percentage of each key frame
    let moments = [NSNumber(value: 0.0), NSNumber(value: 0.1),
                   NSNumber(value: 0.3), NSNumber(value: 0.8), NSNumber(value: 1.0)]
    animation.keyTimes = moments

    imageView.layer.add(animation, forKey: "rotate")
}

【问题讨论】:

标签: ios swift cakeyframeanimation


【解决方案1】:

设置animation.autoreverses = true,将反转动画。

【讨论】:

    【解决方案2】:

    对于逆时针旋转,您可以这样做:

    self.imageView?.transform = CGAffineTransform(rotationAngle: -2*CGFloat.pi)
    

    【讨论】:

      猜你喜欢
      • 2012-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多