现在,我正在使用以下代码从设备的陀螺仪中获取Euler值。这应该如何使用吗?还是没有使用NSTIMER的更好的方法?
- (void)viewDidLoad {
[super viewDidLoad];
CMMotionManager *motionManger = [[CMMotionManager alloc] init];
[motionManger startDeviceMotionUpdates];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:(1/6) target:self selector:@selector(read) userInfo:nil repeats:YES];
}
- (void)read {
CMAttitude *attitude;
CMDeviceMotion *motion = motionManger.deviceMotion;
attitude = motion.attitude;
int yaw = attitude.yaw;
}
您可以使用此...
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error)
{
CMAttitude *attitude;
attitude = motion.attitude;
int yaw = attitude.yaw;
}];
直接引用文档:
以指定的间隔处理运动更新
接收运动数据 在特定的间隔中,该应用称为"开始"方法 行动队列(nsoperationqueue的实例)和一个块处理程序 处理这些更新的特定类型。运动数据是 传递到块处理程序。确定更新的频率 根据"间隔"属性的值。
[...]
设备运动。设置DeviceMotionUpdateInterval属性以指定 更新间隔。致电OR StartDeviceMotionUpdatesusingReferenceFrame:Toqueue:withHandler:或 StartDeviceMotionUpDateStoqueququeququeququeququequequeque:方法:方法,传递 CMDeviceMotionHandler类型的块。使用以前的方法(新的 iOS 5.0),您可以指定用于用于该的参考框架 态度估计。旋转率数据被传递到块中 cmdevicemotion对象。
所以,例如
motionManger.deviceMotionUpdateInterval = 1.0/6.0; // not 1/6; 1/6 = 0
[motionManager
startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:
^(CMDeviceMotion *motion, NSError *error)
{
CMAttitude *attitude;
attitude = motion.attitude;
int yaw = attitude.yaw;
}];
我懒洋洋地使用了主要队列,但这仍然可能比NSTIMER更好,因为它会给运动经理一个明确的线索,以了解您愿意更新的频率。