15

AppleはiPhoneSDK3.0でShakeAPIを発表しました。この新機能に関する情報が見つかりません。

誰がそれを使用する方法を知っていますか?どんな例でも、リンクは良いでしょう。

4

3 に答える 3

36

探している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のサブクラスです)。また、motionBegan:withEvent:ではなく、motionEnded:withEvent:で処理する必要があります。motionBegan:withEvent:は、電話が揺れが発生している疑いがある場合に呼び出されますが、OSは、ユーザーが意図的に揺れているのか、偶発的に揺れているのか(階段を上るなど)の違いを判断できます。OSが、motionBegan:withEvent:が呼び出された後、実際のシェイクではないと判断した場合、motionEnded:withEvent:の代わりにmotionCancelled:を呼び出します。

于 2009-07-23T12:21:30.300 に答える
7

私はこのスレッドで完全な3.0の例を投稿しました:

誰かがiPhoneを振ったことを検出するにはどうすればよいですか?

于 2009-07-23T15:57:50.127 に答える
3

Joe Hewittは最近、3.0シェイクイベントを利用するコードをThree20にコミットしました。内にいくつかの簡単なコードを実装する必要があるようです。-motionBegan:withEvent:UIResponder

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (event.type == UIEventSubtypeMotionShake) {
        ...
    }
}
于 2009-07-23T12:08:43.097 に答える