English 中文(简体)
IPhone accelerometer works even in flat surface
原标题:

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&&currentPoint.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 ?

最佳回答

I notice a couple of things in the code

  • The two first if statements to have the position within a certain range should be done just before the slider position is set, otherwise it can happen that the image will be set outside the preferred range.

  • ballXVelocity is calculated as normalized from the acceleration.y value times the delta time. Maybe you should look into multiplying that factor instead of doing the kVelocityMultiplier on the next line.

  • As the accelerometer is very sensitive and it is hard to make perfect fit on the circuitboard one never get perfect value. Instead one should try to have some calibration stage and perhaps use only a valid range similar the two first if statements.

问题回答

You might consider filtering your values the Apple way.

#define kFilteringFactor 0.15

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
accelx = acceleration.x * kFilteringFactor + accelx * (1.0 - kFilteringFactor);
accely = acceleration.y * kFilteringFactor + accely * (1.0 - kFilteringFactor);
accelz = acceleration.y * kFilteringFactor + accelz * (1.0 - kFilteringFactor);}

accelx, accely and accelz are UIAccelerometerValues.

Then you can do something like

ball.position.x += accelx * ballSpeed * deltaDrawingTime;

The movement should be nicer now.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签