簡體   English   中英

在iOS中以度或弧度為單位的Y軸旋轉

[英]Rotation on Y axis in degree or radians in iOS

如圖所示,我需要在加速度計的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));

任何幫助都感激不盡。

#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.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM