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 :
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 :
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.
NSOperationQueue
. See my answer here for the correct way to setup an NSOperationQueue
. Also, @JohnFontaine, please make the appropriate changes to your answer. –
Buhr © 2022 - 2024 — McMap. All rights reserved.
[email protected]
– Drypoint