ios - 根据 iDevice 磁强计读数旋转 UIView

标签 ios objective-c cllocationmanager cgaffinetransform magnetometer

我有一个包含一些点的 UIView,我通过 CLLocationManager 让它根据磁力计的读数旋转,如下所示:

@interface PresentationVC () {
    float initialBearing;
}
@end

@implementation PresentationVC
- (void)viewDidLoad {
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    [self.locationManager startUpdatingHeading];
    [self.locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
    if(initialBearing == 0) {
        initialBearing = newHeading.magneticHeading;
    }
    NSLog(@"Magnetic Heading: %f", newHeading.magneticHeading);
    viewMap.transform = CGAffineTransformMakeRotation(degreesToRadians(initialBearing - newHeading.magneticHeading));
}

@end

viewMapUIView

上面的代码有效,但我想将 UIView 的变换设置为 0 度/弧度,即 CGAffineTransformMakeRotation(0)。当前初始设置为当前方位,例如157度。

我尝试在上面的代码中使用initialBearing来计算偏移角度,但它最初仍然旋转了一个角度。我错过了什么?

此外,我无法 360 度旋转; CGAffineTransformMakeRotation() 在我几乎转动 180 度时反弹回旋转。如何在不弹跳的情况下进行完整的 360 度旋转? (估计是弧度问题)

最佳答案

最终我发现 DegreeToRadians() 出现故障,导致弧度计算不正确。

// this is malfunctioned
#define degreesToRadians(degrees) (M_PI * degrees / 180.0)

// this is working, thanks @TonyMkenu
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)

关于ios - 根据 iDevice 磁强计读数旋转 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28556027/

相关文章:

ios - 在呈现的 MFMessageComposeViewController 中打开 URL

ios - 如何在 iPhone 中单击按钮时显示 TableView(如下拉菜单)?

objective-c - 应用 CGAffineTransform 后从 imageview 获取新的图像尺寸

objective-c - 从不同的 View Controller 访问属性

iOS:为什么在启动时会显示两次 "Turn on Location Services"警报?

ios - 在 viewDidLoad 中获取位置

ios - 协议(protocol)和委托(delegate)到底是什么,它们在 IOS 中是如何使用的?

ios - GKLeaderboard.localPlayerScore 在 iOS 8.1.3 上返回 nil?

objective-c - iOS7 状态栏与 View 重叠

ios - iBeacon - 当应用程序在区域中启动时没有调用 didEnterRegion