IPhone accelerometer works even in flat surface.
Posted
by Amaresh
on Stack Overflow
See other posts from Stack Overflow
or by Amaresh
Published on 2009-11-30T07:48:05Z
Indexed on
2010/03/25
0:13 UTC
Read the original article
Hit count: 758
iphone
|accelerometer
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 ?
© Stack Overflow or respective owner