gpt4 book ai didi

iphone - 如何在iphone处于加速模式或空闲模式时启动和停止CMMotionManager startAccelerating和stopAccelerating?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:32 25 4
gpt4 key购买 nike

我正在开发与加速相关的应用程序。我正在使用 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];
});

}];
}

请帮助我。

最佳答案

你试过添加这个吗?:

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(stopAccelerometer)
name:UIApplicationDidEnterBackgroundNotification
object:nil];

关于iphone - 如何在iphone处于加速模式或空闲模式时启动和停止CMMotionManager startAccelerating和stopAccelerating?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15612585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com