简体   繁体   English

我如何在我们的项目中使用iPhone的加速度计?

[英]How can i use iPhone's accelerometer in our project?

I want to create simple application on iphone. 我想在iPhone上创建简单的应用程序。 For this purpose i use accelerometer, my object is on the track and whenever it will go outside the track, it will detect if i moved iphone 为此,我使用加速度计,我的物体在轨道上,只要它走出轨道,它就会检测我是否移动了iphone

Used Accelerometer reference from Apple. Apple的二手加速度计参考。 Thats really helpful to me. 那对我真的很有帮助。

See the UIAccelerometer class reference . 请参见UIAccelerometer类参考 There's sample code as well (see the Related sample code section near the top). 也有示例代码(请参阅顶部的“ 相关示例代码”部分)。

copy and pasting directly from this stackoverflow answer 直接从此stackoverflow答案复制并粘贴

The APIs you are looking for are in UIResponder: 您要查找的API在UIResponder中:

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;

Generally you just implement this: 通常,您只需实现以下目的:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
  if (event.type == UIEventSubtypeMotionShake) {
    //Your code here
  }
}

in your UIViewController subclass (UIViewController is a subclass of UIResponder). 在您的UIViewController子类中(UIViewController是UIResponder的子类)。 Also, you want to handle it in motionEnded:withEvent:, not motionBegan:withEvent:. 另外,您想在motionEnded:withEvent:中处理它,而不是motionBegan:withEvent:。 motionBegan:withEvent: is called when the phone suspects shaking is happening, but the OS can determine the difference between a user purposefully shaking, and incidental shaking (like walking up the stairs). 当手机怀疑发生晃动时会调用motionBegan:withEvent:,但操作系统可以确定用户故意晃动与偶然晃动(例如上楼梯)之间的区别。 If the OS decides it was not a real shake after motionBegan:withEvent: is called it will call motionCancelled: instead of motionEnded:withEvent:. 如果操作系统认为在调用motionBegan:withEvent:之后不是真正的抖动,它将调用motionCancelled:而不是motionEnded:withEvent:。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM