如何使用CMMotionActivityManager并接收更新? [英] How to use CMMotionActivityManager and receive updates?

查看:161
本文介绍了如何使用CMMotionActivityManager并接收更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可以接收和处理动作更新的应用程序,以便了解用户是静止,行走,跑步还是在交通工具上。
我在参考文献中看到CMMotionActivityManager对我有用。

I want to create an app that can receive and process motion updates in order to understand if the user is stationary, walking, running or is on a transport. I've seen on the reference that CMMotionActivityManager can be useful for me.


CMMotionActivityManager类提供对运动数据的访问由设备存储。运动数据反映用户是在行走,跑步,在车辆中还是静止一段时间。

The CMMotionActivityManager class provides access to the motion data stored by a device. Motion data reflects whether the user is walking, running, in a vehicle, or stationary for periods of time.

我是应用程序开发的新手,我不明白如何使用该方法开始更新。
执行此操作的方法是 - (void)startActivityUpdatesToQueue:(NSOperationQueue *)队列withHandler:(CMMotionActivityHandler)处理程序
我不明白我应该在处理程序上写什么,因为引用说:

I'm new to app developing and I don't understand how to use the method for starting the updating. The method for doing this is - (void)startActivityUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMMotionActivityHandler)handler. I don't understand what should i write on handler because the reference says:


handler
块到当检测到当前运动类型的变化时执行。有关此块参数的信息,请参阅CMMotionActivityHandler。此属性不得为零。

handler The block to execute when a change in the current type of motion is detected. For information about the parameters of this block, see CMMotionActivityHandler. This property must not be nil.

我的实现是:

- (IBAction)startButtonPressed:(id)sender {
_motionActivityManager = [[CMMotionActivityManager alloc] init];
[_motionActivityManager startActivityUpdatesToQueue:NSOperationQueueDefaultMaxConcurrentOperationCount withHandler:CMMotionActivityHandler];
}

我已经导入了CoreMotion框架
但XCode don' t识别 CMMotionActivityHandler ,我哪里错了?我该如何解决这个问题?

I've already imported the CoreMotion framework But XCode don't recognize CMMotionActivityHandler, where am I wrong? How can I resolve this problem?

谢谢

推荐答案

最高这个答案的投票版有点迂回。它创建一个队列,但随后使用GCD在主队列上执行。此外,许多示例在 withHandler 参数中放置了一个块,但我发现它看起来很笨拙并且看起来不干净(从代码格式的角度来看)。

The highest voted version of this answer is a bit roundabout. It creates a queue, but then uses GCD to execute back on the main queue. Also, many examples put a block within the withHandler parameter, but I find that clunky and doesn't look as clean (from a code formatting perspective).

这是我的示例实现:

@implementation MotionHandler {
@private
    // this is a private variable for this class that is not visible outside
    // (also, iOS handles memory and access management of these faster than properties)
    CMMotionActivityManager *_motionActivityManager;
}

// initialization method, you can do other stuff here too
- (instancetype)init {
    self = [super init];
    if (self) {
        // check to see if the device can handle motion activity
        if ([CMMotionActivityManager isActivityAvailable]) {
            // if so, initialize the activity manager
            _motionActivityManager = [[CMMotionActivityManager alloc] init];
        }
    }
}

- (void)startMotionActivityMonitoring {
    // create the motion activity handler
    CMMotionActivityHandler motionActivityHandler = ^(CMMotionActivity *activity) {
         // TODO motion detected here. Do something.
    }

    // check to see if the motion activity manager exists
    if (_motionActivityManager) {
        // if so, start monitoring activity
        // notice that we add updates to the mainQueue. This will call your handler on the main thread
        [_motionActivityManager startActivityUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:motionActivityHandler];
    }
}

@end

这篇关于如何使用CMMotionActivityManager并接收更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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