[英]UIKit Swift 5: Set UIView Rotation Angle
我正在构建一个具有指南针功能和一些附加功能的应用程序。 我的目标是按照真北旋转指南针UIImageView
。
是否可以为图像设置特定的旋转角度(如在 Photoshop 或 Figma 中)而不是将其旋转一定量?
我当前的实现如下所示:
func rotate(view: UIView, radians: Double) {
UIView.animate(withDuration: 0.5) {
view.transform = view.transform.rotated(by: CGFloat(radians))
}
}
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
let newRad = deg2rad(newHeading.trueHeading)
rotate(view: compassImageView, radians: compassRad-newRad)
compassRad = newRad
}
我希望做一些事情的效果:
compassImageView.layer.setRotationAngle(radians: newHeading.trueHeading)
存在这样的东西吗?
如果我理解正确,你想要:
func rotate(view: UIView, to angle: Double) {
UIView.animate(withDuration: 0.5) {
view.transform = CGAffineTransform(rotationAngle: angle)
}
}
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
let newRad = deg2rad(newHeading.trueHeading)
rotate(view: compassImageView, to: newRad)
compassRad = newRad
}
或者它可能是-newRad
,这取决于这些角度 go 的方式,我永远记不得了:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.