1

我在视图中有一个 imageView。即使iphone静止了一段时间,它也会移动。为什么会这样?此外,图像对 iphone 的移动也没有快速响应。

这是我为此编写的代码:

我还为加速度计设置了 updateInterval 和委托。

#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];
}

谁能帮帮我?

4

2 回答 2

1

您可能会考虑以 Apple 方式过滤您的值。

#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 和 accelz 是 UIAccelerometerValues。

然后你可以做类似的事情

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

现在运动应该更好了。

于 2010-03-25T00:09:04.640 回答
0

我注意到代码中有几件事

  • 将位置置于特定范围内的前两个 if 语句应在设置滑块位置之前完成,否则可能会发生图像将设置在首选范围之外。

  • ballXVelocity计算为从acceleration.y值乘以增量时间归一化。也许你应该考虑乘以那个因素而不是kVelocityMultiplier在下一行做。

  • 由于加速度计非常敏感,并且很难完美地安装在电路板上,因此永远无法获得完美的价值。相反,应该尝试进行一些校准阶段,并且可能只使用类似于前两个 if 语句的有效范围。

于 2009-11-30T08:14:23.747 回答