为什么此CMDeviceMotionHandler从未被CoreMotion调用? [英] Why is this CMDeviceMotionHandler never called by CoreMotion?

查看:70
本文介绍了为什么此CMDeviceMotionHandler从未被CoreMotion调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在项目中包含CoreMotion框架,并将CoreMotion框架导入了视图控制器的标题中:

I've included the CoreMotion framework in my project and imported the CoreMotion framework in the header of my view controller:

#import <CoreMotion/CoreMotion.h>

然后在-viewDidLoad中,我有一个简单的测试代码,该代码在带有iOS 4.3的iPhone 4上运行:

Then in -viewDidLoad I have this simple test code which I run on an iPhone 4 with iOS 4.3:

- (void)viewDidLoad {
    [super viewDidLoad];

    CMMotionManager *motionManager = [[CMMotionManager alloc] init];
    [motionManager setDeviceMotionUpdateInterval:0.1];

    CMDeviceMotionHandler  motionHandler = ^(CMDeviceMotion *motion, NSError *error) {

        NSLog(@"foo");

    };

    [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:motionHandler];

    NSLog(@"merde!");
}

当我在设备上运行项目并四处移动时,我永远都不会收到"foo"日志.另外,我尝试在运动处理程序块中设置一个断点,但它从未在那里停止.但是我确实得到了"merde!"日志,因此该代码一定会执行.

When I run the project on the device and move it around, I never get a "foo" log. Also, I tried setting a breakpoint in the motion handler block and it never halts there. But I do get the "merde!" log, so this code is definitely executed.

为什么核心运动没有呼叫我的处理程序?我的iPhone 4正常运行.加速度计和陀螺仪可在其他应用程序中完美运行.没有硬件错误.

Why is core motion not calling my handler? My iPhone 4 is functional. Accelerometers and gyro work perfectly in other apps. There is no hardware bug.

推荐答案

我成功运行了非常相似的代码,这同样适用于

I had very similar code running successfully, same applies to the available samples in the Event Handling Guide for iOS (there is only an appropriate one for gyro data). But there is one major difference:

所有实现都使用 operationQueue = [[NSOperationQueue currentQueue]保留]; 保留自己对操作队列的引用,或者构建自己的引用.因此,希望这有助于在您的日志中获得比"merde"更多的东西;-)

All implementations hold their own reference to the opereation queue with operationQueue = [[NSOperationQueue currentQueue] retain]; or build their own. So hopefully this helps to get more than 'merde' in your logs ;-)

这篇关于为什么此CMDeviceMotionHandler从未被CoreMotion调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆