[英]How can i use iPhone's accelerometer in our project?
我想在iPhone上创建简单的应用程序。 为此,我使用加速度计,我的物体在轨道上,只要它走出轨道,它就会检测我是否移动了iphone
Apple的二手加速度计参考。 那对我真的很有帮助。
请参见UIAccelerometer类参考 。 也有示例代码(请参阅顶部的“ 相关示例代码”部分)。
直接从此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:。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.