我在我的应用程序中使用CMMotionManager,这样我就可以获得设备运动信息。我有以下两种方法:
- (void)startDeviceMotion {
motionManager = [[CMMotionManager alloc] init];
motionManager.showsDeviceMovementDisplay = YES;
motionManager.deviceMotionUpdateInterval = 1.0 / 120.0;
[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical];
}二是:
- (void)stopDeviceMotion {
[motionManager stopDeviceMotionUpdates];
[motionManager release];
motionManager = nil;
}它们分别在应用程序启动和应用程序完成时启动。我现在的问题是多任务处理。如果我把我的问题放到后台,然后再次将它带到前台,我就会得到一条消息(在NSZombie激活的情况下),它告诉我正在将一条[CMMotionManager retain]消息发送到一个已分配的实例。
我的问题在哪里?
谢谢!
发布于 2012-02-17 08:56:22
尝试使用乔纳森的建议。基本上,为了确保只创建了CMMotionManager的一个实例,将motionManager放在AppDelegate中,并在任何您想使用motionManager的地方通过这个方法检索它。
-(CMMotionManager *)motionManager
{
CMMotionManager *motionManager = nil;
id appDelegate = [UIApplication sharedApplication].delegate;
if ([appDelegate respondsToSelector:@selector(motionManager)]) {
motionManager = [appDelegate motionManager];
}
return motionManager;
}如果这对你有用的话请告诉我。
https://stackoverflow.com/questions/9257942
复制相似问题