繁体   English   中英

如何设置 UIView 按度旋转

[英]How to set UIView rotation by degrees

我试图通过旋转 UIImageView 对象来反映 iOS 应用程序中的加速度计数据。 目前,我正在根据对象当前位置读数和先前位置读数之间的差异来旋转对象(例如,如果加速度计处于 90 度,而现在处于 80 度,它将旋转对象 -10 度)。 但是这种方法有问题,因为加速度计中有未读的变化。

因此,我想根据加速度计的度数方向设置 UIImageView 的旋转 例如,如果加速度计读数为 65 度,我想将 UIImageView 对象的方向设置为 65 度。 有没有办法做到这一点?

此外,我希望旋转动画。

这是我当前的代码,它根据先前阅读的更改旋转图像视图对象:

// UIImage view declaration
@IBOutlet weak var visualizer: UIImageView! 

// Rotation code where 'deg' is the change in rotation from the previous reading
UIView.animate(withDuration: 1.0, delay: 0, options: .curveLinear, animations: { () -> Void in self.visualizer.transform = self.visualizer.transform.rotated(by: CGFloat(truncating: deg))})

请注意,我将更改代码的另一部分,以便 'deg' 将是加速度计的方向(以度为单位),而不是度数的变化。

旋转基于弧度,所以你只需要做数学

let radian = degree * CGFloat.pi / 180

使用 CGAffineTransform 将对象旋转到 n 度

 view.transform = CGAffineTransform(rotationAngle: (degree * CGFloat.pi / 180))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM