在游戏中移动UIViews的最佳方式(无用户交互)

时间:2012-12-25 22:30:10

标签: iphone objective-c core-animation accelerometer move

我正在寻找一种良好且流畅的方式,可以同时在UIViews上移动多个Screen。当Action检测到加速时,Accelerometer应该发生。我已经尝试过这种方式:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{

    CGPoint ObjectPos = Object.center;

    ObjectPos.x -= acceleration.x;

    Object.center = ObjectPos;
}

(当然我添加了一些调整以改善运动检测),但总而言之,它仍然不是很流畅。

我希望核心动画有一些方法,但它似乎不能很好地加速。

非常感谢帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

我建议你阅读这篇文章: Isolating the Gravity Component from Acceleration Data 以及下面名为'隔离瞬时运动与加速数据'的部分。

基本上,您需要过滤掉重力以获得平稳运动。该链接提供了示例代码。

编辑:UIAccelerometer在iOS 5.0中已弃用,随附的文档似乎也已消失。

为了将来的参考,我做了一些挖掘,发现了原始示例代码(source)的“副本”,似乎是:

// This example uses a low-value filtering factor to generate a value that uses 10 percent of the
// unfiltered acceleration data and 90 percent of the previously filtered value


// Isolating the effects of gravity from accelerometer data
#define kFilteringFactor 0.1
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    // Use a basic low-pass filter to keep only the gravity component of each axis.
    accelX = (acceleration.x * kFilteringFactor) + (accelX * (1.0 - kFilteringFactor));
    accelY = (acceleration.y * kFilteringFactor) + (accelY * (1.0 - kFilteringFactor));
    accelZ = (acceleration.z * kFilteringFactor) + (accelZ * (1.0 - kFilteringFactor));
    // Use the acceleration data.
}

// shows a simplified high-pass filter computation with constant effect of gravity.

// Getting the instantaneous portion of movement from accelerometer data
#define kFilteringFactor 0.1
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    // Subtract the low-pass value from the current value to get a simplified high-pass filter
    accelX = acceleration.x - ( (acceleration.x * kFilteringFactor) + (accelX * (1.0 - kFilteringFactor)) );
    accelY = acceleration.y - ( (acceleration.y * kFilteringFactor) + (accelY * (1.0 - kFilteringFactor)) );
    accelZ = acceleration.z - ( (acceleration.z * kFilteringFactor) + (accelZ * (1.0 - kFilteringFactor)) );
    // Use the acceleration data.
}