0

我有一个应用程序,它在加载时会定期获取设备运动更新(每秒 20 次)并使用它们在屏幕上显示内容。使用旧(已弃用)UIAccelerometer代码可以正常工作。

我已经将它移植到使用 Core Motion 并且CMMotionManager但我遇到的问题是每次(除了第一次)加载这些视图中的一个时,在第一秒左右只收到一个或两个更新,然后它们开始按预期接收(每秒 20 个)。

视图中的相关代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Register to receive acceleration from motion manager.
    motionManager = [[CMMotionManager alloc] init];
    motionManager.deviceMotionUpdateInterval = 0.05;
    [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
        withHandler:^(CMDeviceMotion *motion, NSError *error) {
            // Handle motion and update UI here.
        }
    ];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    // Stop the motion updates.
    [motionManager stopDeviceMotionUpdates];
}

- (IBAction)exitView:(id)sender {
    // Stop the motion updates.
    [motionManager stopDeviceMotionUpdates];
    // Pop the view from the navigation controller.
}

我已经添加了一些NSLog语句,并且可以确认视图加载后立即请求运动更新,并且处理程序仅接收如上所述的更新。

我有什么遗漏可以使 CoreMotion 版本的行为与旧版本相同,即没有延迟吗?

4

0 回答 0