【问题标题】:How to rotate image based on angle in Swift?如何在 Swift 中根据角度旋转图像?
【发布时间】:2015-06-20 02:49:22
【问题描述】:

我正在尝试旋转图像以直观地向用户显示方向,就像指南针一样。所以我试图向一个函数提供一个角度,以在角度的方向上旋转图像,但图像只是失去控制,当它最终停止时似乎并不正确。每次委托触发时都会调用此函数:locationManager(manager: CLLocationManager!, didUpdateHeading newHeading: CLHeading!)。有人能指出我正确的方向吗?

func headingDidChange(headingAngle: Double) {
    UIView.animateWithDuration(1.0, animations: {
        self.compassImage.transform = CGAffineTransformMakeRotation(CGFloat(headingAngle))
    })
}

【问题讨论】:

  • 我认为您指定 headingAngle 以度为单位,但 CGAffineTransformMakeRotation 采用弧度。
  • 应将变换应用于原始图像(零旋转)。如果它反复应用于修改后的图像,它将失控。

标签: ios swift animation ios8 core-animation


【解决方案1】:

在委托方法中尝试以下代码来旋转您的图像。

    let oldRad:Float = 1.259374
    let newRad:Float = 1.239832

    let theAnimation = CABasicAnimation(keyPath: "transform.rotation")

    theAnimation.fromValue = NSNumber(float: oldRad)
    theAnimation.toValue = NSNumber(float: newRad)

    theAnimation.duration = 0.2

    self.compassImage!.layer.addAnimation(theAnimation, forKey: "animateMyRotation")

    self.compassImage!.transform = CGAffineTransformMakeRotation(CGFloat(newRad))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-12
    • 2017-04-14
    • 2015-01-08
    • 2012-07-02
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多