题
我有一个图片视图。它的移动,即使是仍然对于一些时间。为什么会这样呢?还有的图像不迅速作出反应的运动。
这里是我的代码编写的,为此:
我们还设置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&¤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];
}
谁能帮帮我吗?
解决方案
我注意到几件事情在代码
两个第一,如果声明具有的位置,在一定范围内应该做的只是之前的位置滑落,否则就可能发生的图像将被置之外的优选的范围。
ballXVelocity
计算为归一化,从acceleration.y
值倍增量时间。也许你应该看看乘以这个因素而不是做的kVelocityMultiplier
在下一个线。为加速度计是非常敏感,这是很难做出完美的结合在一个电路板从来没有得到完美的价值。而不是应该尝试了一些标定阶段,或许仅使用一个有效范围类似的两个第一,如果发言。
其他提示
您可以考虑以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;
现在运动应该更好。
不隶属于 StackOverflow