Y 轴上的旋转度数或弧度 iOS

Rotation on Y axis in degree or radians in iOS

我需要在竖屏模式下获取 iDevice 的旋转,围绕加速度计的 Y 轴,如图所示,(参考:https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/motion_event_basics/motion_event_basics.html

如何绕 Y 轴进行正向或负向旋转。我已经从 Whosebug/Apple 代码中实现了一些代码,当我在 Y 轴上旋转时,滚动、俯仰和偏航没有区别。

[self.motionManager setGyroUpdateInterval:0.1f];
[self.motionManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData *gyroData, NSError *error) {
     dispatch_async(dispatch_get_main_queue(), ^{
          NSString *strYaw = [NSString stringWithFormat:@"Yaw: %.02f",gyroData.rotationRate.z];
          [self.lblYaw setText:strYaw];

          NSString *strPitch = [NSString stringWithFormat:@"Pitch: %.02f",gyroData.rotationRate.x];
          [self.lblPitch setText:strPitch];

          NSString *strRoll = [NSString stringWithFormat:@"Roll: %.02f",gyroData.rotationRate.y];
          [self.lblRoll setText:strRoll];

      });

 }];

还有这个,

roll  = atan2(2*y*w - 2*x*z, 1 - 2*y*y - 2*z*z);
pitch = atan2(2*x*w - 2*y*z, 1 - 2*x*x - 2*z*z);
yaw   =  asin(2*x*y + 2*z*w);

myRoll = (atan2(2*(quat.y*quat.w - quat.x*quat.z), 1 - 2*quat.y*quat.y - 2*quat.z*quat.z));
myPitch = (atan2(2*(quat.x*quat.w + quat.y*quat.z), 1 - 2*quat.x*quat.x - 2*quat.z*quat.z));
myYaw = (asin(2*quat.x*quat.y + 2*quat.w*quat.z));

任何帮助将不胜感激。

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>

@interface ViewController ()
@property (strong, nonatomic) CMMotionManager *motionManager;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.motionManager = [[CMMotionManager alloc] init];
    self.motionManager.accelerometerUpdateInterval = .2;
    self.motionManager.gyroUpdateInterval = .2;

    [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
                                             withHandler:^(CMAccelerometerData  *accelerometerData, NSError *error) {
                                                 [self outputAccelertionData:accelerometerData.acceleration];
                                                 if(error){
                                                      NSLog(@"%@", error);
                                                 }
                                             }];

    [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue]
                                    withHandler:^(CMGyroData *gyroData, NSError *error) {
                                        [self outputRotationData:gyroData.rotationRate];
                                    }];
}

-(void)outputAccelertionData:(CMAcceleration)acceleration
{

}
-(void)outputRotationData:(CMRotationRate)rotation
{
    NSLog(@"(%f)",rotation.y);
}

@end

玩得开心。

我使用 Compass 找到了设备的旋转,创建了 locationManager

/**
 *  Location manager class instance
 */
@property (strong, nonatomic) CLLocationManager *locationManager;

然后初始化

- (void) initLocationManager
{
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.headingFilter = 0.1;
    self.locationManager.delegate = self;
    [self.locationManager startUpdatingHeading];
}

并实现委托方法

- (void) locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
   // newHeading.trueHeading is the property that i needed.
}