iOS detect movement of user

[亡魂溺海] 提交于 2019-11-27 01:41:47

问题


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

demo :


回答1:


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

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.



来源:https://stackoverflow.com/questions/14176281/ios-detect-movement-of-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!