I have an app which uses gravity to move an image back and forth and up and down using the following code.
numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:motion.gravity.x / 16.0 *200]];
numberStringy = [formatter stringFromNumber:[NSNumber numberWithFloat:motion.gravity.y / 16.0 *200]];
n = [numberString intValue];
y = [numberStringy intValue];
self.ball.center = CGPointMake(self.ball.center.x + n, 9);
self.ball.center = CGPointMake(87, self.ball.center.y + y);
I then stop the image moving when it is at a specific place with this code.
stop21.center = CGPointMake(237, 508);
if (ball.center.y > 481 && ball.center.y < 508) {
ball.center = CGPointMake(237, 481);
}
if (ball.center.y >508 && ball.center.y < 535) {
ball.center = CGPointMake(237,535);
}
this works ok to stop the image moving in the direction the device is tilted but when tilted back the other direction the ball will move in that direction.
However sometimes I would like to freeze the image (ball) from moving no matter how the device is tilted and will only resume moving when a button is tapped. I'm not sure how to do this. I have tried to changing the value of x or y by setting y = 0; such as this.
if (ball.center.y >508 && ball.center.y < 535) {
ball.center = CGPointMake(237,535);
y = 0;
}
but this doesn't seem to work.
Does anyone have any suggestions?
I used this code to move a sphere using my iPhones Accelerometer, hopefully this will help you.