使用 UIRotationGestureRecognizer 将弧度转换为度数?

Converting Radians to Degrees using UIRotationGestureRecognizer?

我正在使用 UIRotationGestureRecognizer 旋转图像。我想将旋转角度从弧度转换为度数,因为我可以更好地考虑度数。我在堆栈和其他资源上找到了解决方案,但由于某种原因,该解决方案似乎不起作用

例如,当我将图像逆时针旋转约 45 度时,从公式中得到的度数约为 -0.15???

@objc func handleImageRotation(sender: 
UIRotationGestureRecognizer){
    guard sender.view != nil else{return}

    if sender.state == .began || sender.state == .changed {
        // rotation enacted
        imageView.transform = imageView.transform.rotated(by: sender.rotation)
        rotationAngleRads = sender.rotation
        rotationAngleDegs = rad2Deg(radians: rotationAngleRads)
        print("Degrees: \(rotationAngleDegs!)")

        sender.rotation = 0
     }
}


 // Convert Radians to Degress
private func rad2Deg(radians: CGFloat) -> Double{

    let angle = Double(radians) * (180 / .pi)
    return Double(angle)
}

您的主要问题是您正在重置手势的 rotation 属性。你不应该那样做。来自文档:

The rotation value is a single value that varies over time. It is not the delta value from the last time that the rotation was reported. Apply the rotation value to the state of the view when the gesture is first recognized—do not concatenate the value each time the handler is called.

所以删除 sender.rotation = 0 行。

因此,您需要修复将变换应用到图像视图的方式。

替换:

imageView.transform = imageView.transform.rotated(by: sender.rotation)

与:

imageView.transform = CGAffineTransform(rotatationAngle: sender.rotation)

应用完整旋转而不是尝试增加当前旋转。