iOS检测用户的移动 [英] iOS detect movement of user

查看:123
本文介绍了iOS检测用户的移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的应用程序,当我在Y轴上将手机从起点移动到终点(例如从点a(0,0)到点b(0)时,在屏幕上画一条简单的线) ,10)请帮助

I want to create a simple app that draws a simple line on screen when I move my phone on the Y-axis from a start point to end point, for example from point a(0,0) to point b(0, 10) please help

演示:

推荐答案

您需要初始化运动管理器,然后检查motion.userAcceleration.y值以获取适当的加速度值(以米/秒/秒为单位).

You need to initialize the motion manager and then check motion.userAcceleration.y value for an appropriate acceleration value (measured in meters / second / second).

在下面的示例中,我检查了0.05,我发现这是手机相当不错的向前移动.我还要等到用户显着放慢速度(-Y值)后再绘制.调整设备MotionUpdateInterval将确定您的应用对速度变化的响应能力.目前,它以1/60秒的速度采样.

In the example below I check for 0.05 which I've found is a fairly decent forward move of the phone. I also wait until the user slows down significantly (-Y value) before drawing. Adjusting the device MotionUpdateInterval will will determine the responsiveness of your app to changes in speed. Right now it is sampling at 1/60 seconds.

motionManager = [[CMMotionManager alloc] init];
motionManager.deviceMotionUpdateInterval = 1.0/60.0;
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
    NSLog(@"Y value is: %f", motion.userAcceleration.y);
    if (motion.userAcceleration.y > 0.05) { 
        //a solid move forward starts 
        lineLength++; //increment a line length value
    } 
    if (motion.userAcceleration.y < -0.02 && lineLength > 10) {
        /*user has abruptly slowed indicating end of the move forward.
         * we also make sure we have more than 10 events 
         */
        [self drawLine]; /* writing drawLine method
                          * and quartz2d path code is left to the 
                          * op or others  */
        [motionManager stopDeviceMotionUpdates];
    }
}];

请注意,此代码假设手机平放或略微倾斜,并且用户在纵向模式下向前推动(远离自己或与手机一起移动).

Note this code assumes that the phone is lying flat or slightly tilted and that the user is pushing forward (away from themselves, or moving with phone) in portrait mode.

这篇关于iOS检测用户的移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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