Detect iPhone shake

Source: Internet
Author: User

Http://stackoverflow.com/questions/150446/how-do-i-detect-when-someone-shakes-an-iphone

 

First, Kendall's July 10th answer is spot-on.

Now... I wanted to do something similar (in iPhone OS 3.0 +), only in my case I wanted it app-wide so I cocould alertVariousParts of the app when a shake occurred. Here's what I ended up doing.

First, I subclassedUiwindow. This is easy peasy. Create a new class file with an interface suchMotionWindow : UIWindow(Feel free to pick your own, natch). Add a method like so:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { 
    if (event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) { 
        [[NSNotificationCenter defaultCenter] postNotificationName:@"DeviceShaken" object:self]; 
    } 

Change@"DeviceShaken"To the notification name of your choice. Save the file.

Now, if you use a mainwindow. XIB (stock xcode template stuff), go in there and change the class of your window object fromUiwindowToMotionwindowOr whatever you called it. Save the XIB. If you set upUiwindowProgrammatically, use your new window class there instead.

Now your app is using the specializedUiwindowClass. Wherever you want to be told about a shake, sign up for them notifications! Like this:

[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(deviceShaken) name:@"DeviceShaken" object:nil]; 

To remove yourself as an observer:

[[NSNotificationCenter defaultCenter] removeObserver:self]; 

I put mine inViewwillappear:AndViewwilldisappear:Where view controllers are concerned. be sure your response to the shake event knows if it is "already in progress" or not. otherwise, if the device is shaken twice in succession, you'll have a li 'l traffic jam. this way you can ignore other notifications until you're truly done responding to the original notification.

Also: You may choose to cue offMotionbeganVs.Motionended. It's up to you. In my case, the effect always needs to take placeAfterThe device is at rest (vs. When it starts shaking), so I useMotionended. Try both and see which one makes more sense... or detect/sort y for both!

One more (curious ?) Observation here: notice there's no sign of first responder management in this code. I 've only tried this with table view controllers so far and everything seems to work quite nicely together! I can't vouch for other scenarios though.

Kendall, et. al-Can anyone speak to why this might be soUiwindowSubclasses? Is it because the window is at the top of the food chain?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.