【问题标题】:Rotation on Y axis in degree or radians in iOS在 iOS 中以度数或弧度为单位在 Y 轴上旋转
【发布时间】:2015-03-04 14:37:04
【问题描述】:

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

我怎样才能围绕 Y 轴进行旋转,无论是正数还是负数。我已经从 stackoverflow/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));

任何帮助将不胜感激。

【问题讨论】:

    标签: ios rotation accelerometer gyroscope core-motion


    【解决方案1】:
    #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
    

    玩得开心。

    【讨论】:

    • 旋转率,据我所知,设备旋转时的速度,无论方向如何,当设备稳定时,它再次变为 0。
    • developer.apple.com/library/prerelease/ios/documentation/… Y 轴旋转速度,单位为弧度/秒。符号遵循右手定则:如果右手绕 Y 轴,拇指尖指向 Y 轴正方向,则正向旋转是指向其他四个手指尖的方向。
    • 我需要的是持有设备的角度。不是设备旋转的速率,因为当设备稳定时它变为 0,因此无法计算出它的角度。对吗?
    【解决方案2】:

    我使用 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.
    }
    

    【讨论】:

      猜你喜欢
      • 2010-11-02
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多