【问题标题】:Rotate UIView according to iDevice Magnetometer reading根据 iDevice 磁力计读数旋转 UIView
【发布时间】:2015-02-18 10:51:52
【问题描述】:

我有一个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 度;当我转近 180 度时,CGAffineTransformMakeRotation() 会反弹旋转。如何在不弹跳的情况下旋转完整的 360 度旋转? (我猜是关于弧度的问题)

【问题讨论】:

标签: ios objective-c cllocationmanager cgaffinetransform magnetometer


【解决方案1】:

最后我发现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)

【讨论】:

  • 欢迎您,很高兴为您提供帮助! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多