Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Shake API in iPhone SDK 3.0?

Apple annonced Shake API in iPhone SDK 3.0. I can not find any information regarding this new feature.

Who knows about how to use it? Any example, link will be good.

like image 204
sashaeve Avatar asked Jul 23 '09 10:07

sashaeve


2 Answers

The APIs you are looking for are in 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). Also, you want to handle it in motionEnded:withEvent:, not 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). If the OS decides it was not a real shake after motionBegan:withEvent: is called it will call motionCancelled: instead of motionEnded:withEvent:.

like image 71
Louis Gerbarg Avatar answered Sep 30 '22 17:09

Louis Gerbarg


I posted a complete 3.0 example in this thread:

How do I detect when someone shakes an iPhone?

like image 20
Kendall Helmstetter Gelner Avatar answered Sep 30 '22 19:09

Kendall Helmstetter Gelner