1

コードに奇妙な問題があり、理解できず、助けが必要です。AppDelegateでCMMotionManagerを定義しました。ビューコントローラで、プロパティを定義します。

    @property (nonatomic, retain) CMMotionManager *motionManager;

次に、このコードを使用して更新を開始します。

    - (void)startMyMotionDetect
    {
motionManager = [(AppDelegate *)[[UIApplication sharedApplication] delegate] sharedManager];

motionManager.deviceMotionUpdateInterval = 1.0/60.0;

[motionManager startDeviceMotionUpdates];
timer = [NSTimer scheduledTimerWithTimeInterval:20/60 target:self selector:@selector(handleDeviceMotion) userInfo:nil repeats:YES];

}

ただし、handleDeviceMotionは数回だけ呼び出され、その後deviceMotion出力の更新を停止します。コードをプッシュスタイル(ブロックコード)に変更しましたが、その場合、ブロックは実行されません!ブロック内のテストNSLogコメントは実行されません。そのコードは次のようになります。

motionManager = [(AppDelegate *)[[UIApplication sharedApplication] delegate] sharedManager];

motionManager.deviceMotionUpdateInterval = 1.0/60.0;

[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *dMotion, NSError *error)
 {
     NSLog(@"test 1");

    [self performSelectorOnMainThread:@selector(handleDeviceMotion) withObject:nil waitUntilDone:YES];

 }];

handleDeviceMotionコードは次のようになります。

- (void)handleDeviceMotion//:(CMDeviceMotion *)dMotion
{

if ([motionManager isDeviceMotionActive])
{
    NSLog(@"test");
}

....}

最初の方法では、「テスト」が数回印刷された後、停止します。

motionManagerをプロパティとして保持することで、リリースされることを心配する必要はないと思いましたが、どこかでリリースされているようです(または何か他のことが起こっていると思いますか?)。

助けてくれて本当にありがとうございます。

4

0 に答える 0