我需要在纵向模式下围绕加速度计的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));
非常感谢任何帮助。
答案 0 :(得分: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
玩得开心玩。
答案 1 :(得分:0)
我发现使用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.
}