哪种方式最适合检测滚动和俯仰?我尝试使用此代码:
- (void)viewDidLoad {
[super viewDidLoad];
self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.accelerometerUpdateInterval = 1/60;
[self.motionManager startAccelerometerUpdatesToQueue:self.queue withHandler:
^(CMAccelerometerData *accelerometerData, NSError *error) {
[(id) self setAcceleration:accelerometerData.acceleration];
[self performSelectorOnMainThread:@selector(update) withObject:nil waitUntilDone:NO];
}];
}
- (void)update {
float accX = atan2f(self.acceleration.x, self.acceleration.z) * 180 /M_PI;
float accY = atan2f(self.acceleration.y, self.acceleration.z) * 180 /M_PI;
//convert to 360 degrees
if (accX < 0) {
accX = 360 + accX;
}
if (accY < 0) {
accY = 360 + accY;
}
NSLog(@"X: %f Y: %f",accX,accY);
}
当设备(iPhone)在桌面上时,值为X:180和Y:180,但是当我将设备放在右侧时X大约为90,但是Y在0-360之间是混乱的。
我想用它在4个方向上移动物体。它应该像pac-mac一样移动,但使用加速度计。当设备处于水平位置时,它可以正常工作,但是当设备处于垂直位置时,只能在两个方向上工作。有没有更好的方法来解决它?