设备转向

时间:2013-08-14 19:27:00

标签: ios objective-c core-motion

如何确定设备转向?

例如,我想这样做

if ( the device turn right ) {
// do something
}

else if ( the device turn left ) {
// do something
}

到目前为止,我已完成以下代码:

[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(Motion) userInfo:nil repeats:YES];

-(void)Motion{


#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))



CMDeviceMotion *currentDeviceMotion = motionManager.deviceMotion;
CMAttitude *currentAttitude = currentDeviceMotion.attitude;

float yaw = roundf((float)(RADIANS_TO_DEGREES(currentAttitude.yaw)));
 positionIn360 = yaw;
if (positionIn360 < 0)
{
    positionIn360 = 360 + positionIn360;
}

1 个答案:

答案 0 :(得分:0)

查看CMDeviceMotion Class Reference。有几种方法可以使用这个框架计算设备转向/倾斜的方向。