嗨 我必須實現一個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