【发布时间】:2011-02-09 22:07:47
【问题描述】:
我想在 iphone 上创建简单的应用程序。为此,我使用加速度计,我的对象在轨道上,只要它超出轨道,它就会检测我是否移动了 iphone
【问题讨论】:
标签: iphone
我想在 iphone 上创建简单的应用程序。为此,我使用加速度计,我的对象在轨道上,只要它超出轨道,它就会检测我是否移动了 iphone
【问题讨论】:
标签: iphone
使用 Apple 提供的加速度计参考。这对我真的很有帮助。
【讨论】:
请参阅UIAccelerometer class reference。还有示例代码(请参阅顶部附近的相关示例代码部分)。
【讨论】:
直接从此stackoverflow答案复制和粘贴
您正在寻找的 API 在 UIResponder 中:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
通常你只需实现这个:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventSubtypeMotionShake) {
//Your code here
}
}
在您的 UIViewController 子类中(UIViewController 是 UIResponder 的子类)。此外,您希望在 motionEnded:withEvent: 中处理它,而不是在 motionBegan:withEvent: 中处理。 motionBegan:withEvent: 当手机怀疑正在发生晃动时调用,但操作系统可以确定用户故意晃动和偶然晃动(如走上楼梯)之间的区别。如果操作系统在调用 motionBegan:withEvent: 后确定它不是真正的抖动,它将调用 motionCancelled: 而不是 motionEnded:withEvent:。
【讨论】: