我正在开发与加速相关的应用程序。我正在使用CMMtionManager
对象。但是当我调用startaccelerating
方法时,它的工作并连续调用该方法,即使 iphone 在空闲模式下也是如此。如何在 iphone 加速和空闲时停止和开始加速。
- (void)viewDidLoad
{
[super viewDidLoad];
motionManager=[[CMMotionManager alloc]init];
motionManager.accelerometerUpdateInterval=2;
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self startMyMotionDetect];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[motionManager stopAccelerometerUpdates];
// Request to stop receiving accelerometer events and turn off accelerometer
}
- (CMMotionManager *)motionManager
{
motionManager = nil;
id appDelegate = [UIApplication sharedApplication].delegate;
if ([appDelegate respondsToSelector:@selector(motionManager)]) {
motionManager = [appDelegate motionManager];
}
return motionManager;
}
- (void)startMyMotionDetect
{ NSLog(@"active %d",motionManager.accelerometerActive);
NSLog(@"availabilty %d",motionManager.accelerometerAvailable);
[motionManager
startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
withHandler:^(CMAccelerometerData *data, NSError *error)
{
dispatch_async(dispatch_get_main_queue(),
^{
NSLog(@"hello");
[motionManager stopAccelerometerUpdates];
});
}];
}
请帮助我。