如何在iPhone SDK 3.0中使用Shake API?

sas*_*eve 15 iphone accelerometer shake

Apple在iPhone SDK 3.0中宣布了Shake API.我找不到有关此新功能的任何信息.

谁知道如何使用它?任何例子,链接都会很好.

Lou*_*arg 36

您正在寻找的API位于UIResponder中:

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
Run Code Online (Sandbox Code Playgroud)

通常你只是实现这个:

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

在你的UIViewController子类中(UIViewController是UIResponder的子类).此外,您想在motionEnded中处理它:withEvent:,而不是motionBegan:withEvent:.motionBegan:withEvent:当电话怀疑正在发生晃动时被调用,但操作系统可以确定用户故意晃动和偶然震动(如走上楼梯)之间的差异.如果操作系统在motionBegan:withEvent:被调用后判定它不是真正的震动,它将调用motionCancelled:而不是motionEnded:withEvent:.


Ken*_*ner 7

我在这个帖子中发布了一个完整的3.0示例:

如何检测有人摇动iPhone的时间?