【问题标题】:How to use Shake API in iPhone SDK 3.0?如何在 iPhone SDK 3.0 中使用 Shake API?
【发布时间】:2010-11-13 08:14:45
【问题描述】:

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

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

【问题讨论】:

    标签: iphone accelerometer shake


    【解决方案1】:

    Joe Hewitt 最近committedThree20 发送了一些代码,该代码利用了 3.0 抖动事件。似乎您只需要在您的UIResponder 内部的-motionBegan:withEvent: 内实现一些简单的代码。

    - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
        if (event.type == UIEventSubtypeMotionShake) {
            ...
        }
    }
    

    【讨论】:

      【解决方案2】:

      您要查找的 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:。

      【讨论】:

      • 如果我说我们需要在需要摇动手势工作的视图上添加代码[self becomeFirstResponder]; 对吗?
      • 你这样说是对的。另外:(BOOL)canBecomeFirstResponder {return YES;}
      【解决方案3】:

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

      How do I detect when someone shakes an iPhone?

      【讨论】:

      • 继续阅读,我对该帖子的回复确实使用了 3.0。我使用了上面发布的事件,只是我还解释了需要将 UIView 设置为第一响应者,这对于它实际工作至关重要。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 2011-03-17
      • 2010-11-02
      • 1970-01-01
      • 2010-11-06
      相关资源
      最近更新 更多