IPhone accelerometer works even in flat surface.
- by Amaresh
I have an imageView in the view. It moves even if the iphone is still for some time. Why is it so ? Also the image does not respond quickly to the movement of the iphone.
Here is my code written for this:
I have also set the updateInterval and delegate for the accelerometer.
#define kVelocityMultiplier 1000;
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
if(currentPoint.x < 0)
{
currentPoint.x=0;
ballXVelocity=0;
}
if(currentPoint.x > 480-sliderWidth)
{
currentPoint.x=480-sliderWidth;
ballXVelocity=0;
}
static NSDate *lastDrawTime;
if(currentPoint.x<=480-sliderWidth&¤tPoint.x>=0)
{
if(lastDrawTime!=nil)
{
NSTimeInterval secondsSinceLastDraw=-([lastDrawTime timeIntervalSinceNow]);
ballXVelocity = ballXVelocity + -acceleration.y*secondsSinceLastDraw;
CGFloat xAcceleration=secondsSinceLastDraw * ballXVelocity * kVelocityMultiplier;
currentPoint = CGPointMake(currentPoint.x + xAcceleration, 266);
}
slider.frame=CGRectMake(currentPoint.x, currentPoint.y, sliderWidth, 10);
}
[lastDrawTime release];
lastDrawTime=[[NSDate alloc]init];
}
Can anyone help me out please ?