In xcode 5 I am using coremotion to get the accelerometer data and then using that to move an object right/left and up/down on my screen. The right left works perfectly because the 0 center point of the x axis is straight up. However most people hold their phones at about a .-75 angle on the y axis. I would like to reset this as the 0 axis so that when tilts the phone back from this point the object moves down and when they tilt it forward from this point (toward 90 degrees) the object moves up. I have tried setting the min and max for the y center to various points but no matter what I do the object only moves in relation to the original y 0 which is straight up. Here is my code.
in view did load
self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.accelerometerUpdateInterval = .05;
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
[self outputAccelertionData:accelerometerData.acceleration];
if(error){
NSLog(@"%@", error);
}
}];
and separate
-(void)outputAccelertionData:(CMAcceleration)acceleration
{
delta.x = acceleration.x * 10;
delta.y = acceleration.y * 10;
imageView2.center = CGPointMake(imageView2.center.x - delta.x, imageView2.center.y + delta.y);
if (imageView2.center.x < 145) {
imageView2.center = CGPointMake(145, imageView2.center.y);
}
if (imageView2.center.x > 165) {
imageView2.center = CGPointMake(165, imageView2.center.y);
}
if (imageView2.center.y < 0) {
imageView2.center = CGPointMake( imageView2.center.x,0);
}
if (imageView2.center.y >600) {
imageView2.center = CGPointMake(imageView2.center.x,600);
NSLog(@"imageViewcenter%f",imageView2.center.y);
}
}
thanks for any help.