【问题标题】:ios UIImageView rotation direction in swift at variable degree in clockwise directionios UIImageView 以顺时针方向以可变角度快速旋转方向
【发布时间】:2019-03-18 09:15:42
【问题描述】:

我正在通过这段代码旋转图像

UIView.animate(withDuration: 2) {

        self.blueNeedleImageView.transform = CGAffineTransform(rotationAngle: CGFloat(myDegreeValue * Double.pi / 180))

    }

问题是它以最短路径旋转。我想总是顺时针旋转,旋转度数是可变的。 thisthis 没有提供解决方案。

【问题讨论】:

  • 请注意:角度以弧度为单位。
  • iPeter 我通过乘以 (Double.pi / 180) 将角度转换为弧度
  • 最短路径是什么意思?
  • 最短路径的意思是如果想旋转 20 度,那么它将顺时针方向旋转,但如果我想旋转 340 度,那么它将逆时针方向旋转。

标签: ios animation rotation direction


【解决方案1】:

如果角度超过 180°,则将其拆分为两个动画。

【讨论】:

  • 不错的解决方法。但没有合适的方法,因为这种解决方案使事情复杂化。
  • 这似乎是最简单的解决方案。请注意,您可以使用 .transform = CGAffineTransformIdentity 返回您的视图
【解决方案2】:

您可以在CoreAnimation 的帮助下根据您的要求旋转图像,如下所示:

let angle = myDegreeValue
if let n = NumberFormatter().number(from: angle) {
    let floatAngle = Double(truncating: n)
    CATransaction.begin()
    let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
    rotationAnimation.byValue = NSNumber(value: floatAngle * (Double.pi / 180))
    rotationAnimation.duration = 2
    rotationAnimation.isRemovedOnCompletion = true

    CATransaction.setCompletionBlock {
          self.blueNeedleImageView.transform = CGAffineTransform(rotationAngle: CGFloat(floatAngle * (Double.pi / 180)))
    }
    self.blueNeedleImageView.layer.add(rotationAnimation, forKey: "rotationAnimation")
    CATransaction.commit()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多