嗨 我必须实现一个iphone加速计应用程序,在该应用程序中我必须移动基于加速度计坐标的图像。我的应用程序运行良好,但有时我的ImageView会移到顶部(查看y)然后消失。iphone加速计运动不流畅
我用下面的代码,
UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = 1.0f/30.f;
#define kFilteringFactor 0.1
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
static UIAccelerationValue rollingX = 0.0;
static UIAccelerationValue rollingY = 0.0;
// Subtract the low-pass value from the current value to get a simplified high-pass filter
rollingX = (acceleration.x * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
rollingY = (acceleration.y * kFilteringFactor) + (rollingY * (1.0 - kFilteringFactor));
double accelX = acceleration.x - rollingX;
double accelY = acceleration.y - rollingY;
// Use the acceleration data.
float newX = containerView.center.x + ((float)accelX * 30.0f);
float newY = containerView.center.y + ((float)accelY * 30.0f);
containerView.center = CGPointMake(newX, newY);
}
PLZ给我就提前同 感谢帮助。
非常感谢。你能帮我解决我的问题吗? – nivrutti 2011-03-30 07:40:32