How do I detect when someone shakes an iPhone?
Asked Answered
U

17

346

I want to react when somebody shakes the iPhone. I don't particularly care how they shake it, just that it was waved vigorously about for a split second. Does anyone know how to detect this?

Ureide answered 29/9, 2008 at 20:14 Comment(0)
R
293

In 3.0, there's now an easier way - hook into the new motion events.

The main trick is that you need to have some UIView (not UIViewController) that you want as firstResponder to receive the shake event messages. Here's the code that you can use in any UIView to get shake events:

@implementation ShakingView

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if ( event.subtype == UIEventSubtypeMotionShake )
    {
        // Put in code here to handle shake
    }

    if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] )
        [super motionEnded:motion withEvent:event];
}

- (BOOL)canBecomeFirstResponder
{ return YES; }

@end

You can easily transform any UIView (even system views) into a view that can get the shake event simply by subclassing the view with only these methods (and then selecting this new type instead of the base type in IB, or using it when allocating a view).

In the view controller, you want to set this view to become first responder:

- (void) viewWillAppear:(BOOL)animated
{
    [shakeView becomeFirstResponder];
    [super viewWillAppear:animated];
}
- (void) viewWillDisappear:(BOOL)animated
{
    [shakeView resignFirstResponder];
    [super viewWillDisappear:animated];
}

Don't forget that if you have other views that become first responder from user actions (like a search bar or text entry field) you'll also need to restore the shaking view first responder status when the other view resigns!

This method works even if you set applicationSupportsShakeToEdit to NO.

Ridden answered 10/7, 2009 at 21:6 Comment(12)
This didn't quite work for me: I needed to override my controller's viewDidAppear instead of viewWillAppear. I'm not sure why; maybe the view needs to be visible before it can do whatever it does to start receiving the shake events?Paddlefish
I noticed in 3.1 something seemed funny, that might have been it - it could be the view will not take first responder status until that point (or it is a bug). I'll research and file a radar or update my code.Ridden
Odd, I tested again (under 3.0) and the code works- though in 3.1, in the simulator, it stopped working as I had it (still works on the device though).Ridden
Wait a sec, you ARE using canBecomeFirstResponder. :) I'll try this again, only I might move it to UIWindow (if that's even advisable from a performance POV?).Briefing
Great answer, very helpful to put a C#/MonoTouch sample together conceptdev.blogspot.com/2009/10/…Poly
Since the viewController is not a view, I would have thought it could not be a part of the responder chain. I'll have to figure out why that works...Ridden
Here's a weird one, when I run a shake gesture in the simulator the motionEnded:withEvent: method gets called twice with UIEventSubtypeMotionShake. Any idea why this would be getting called more than once?Avogadro
This is easier but not necessarily better. If you want to detect a long, continuous shake this approach is not useful as the iPhone likes to fire motionEnded before the shake has actually stopped. So using this approach you get a disjointed series of short shakes instead of one long one. The other answer works much better in this case.Touching
@Kendall - UIViewControllers implement UIResponder and are in the responder chain. The topmost UIWindow is as well. developer.apple.com/library/ios/DOCUMENTATION/EventHandling/…Narration
[super respondsToSelector: will not do what you want, since it's equivalent to calling [self respondsToSelector: which will return YES. What you need is [[ShakingView superclass] instancesRespondToSelector:.Kultur
That is a really good point, the code would always be called as the if statement would always equate to true.Ridden
Instead of using: if ( event.subtype == UIEventSubtypeMotionShake ) you can use: if ( motion == UIEventSubtypeMotionShake )Donnelly
N
180

From my Diceshaker application:

// Ensures the shake is strong enough on at least two axes before declaring it a shake.
// "Strong enough" means "greater than a client-supplied threshold" in G's.
static BOOL L0AccelerationIsShaking(UIAcceleration* last, UIAcceleration* current, double threshold) {
    double
        deltaX = fabs(last.x - current.x),
        deltaY = fabs(last.y - current.y),
        deltaZ = fabs(last.z - current.z);

    return
        (deltaX > threshold && deltaY > threshold) ||
        (deltaX > threshold && deltaZ > threshold) ||
        (deltaY > threshold && deltaZ > threshold);
}

@interface L0AppDelegate : NSObject <UIApplicationDelegate> {
    BOOL histeresisExcited;
    UIAcceleration* lastAcceleration;
}

@property(retain) UIAcceleration* lastAcceleration;

@end

@implementation L0AppDelegate

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    [UIAccelerometer sharedAccelerometer].delegate = self;
}

- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

    if (self.lastAcceleration) {
        if (!histeresisExcited && L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.7)) {
            histeresisExcited = YES;

            /* SHAKE DETECTED. DO HERE WHAT YOU WANT. */

        } else if (histeresisExcited && !L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.2)) {
            histeresisExcited = NO;
        }
    }

    self.lastAcceleration = acceleration;
}

// and proper @synthesize and -dealloc boilerplate code

@end

The histeresis prevents the shake event from triggering multiple times until the user stops the shake.

Neron answered 1/10, 2008 at 20:43 Comment(8)
Note I have added an answer below presenting an easier method for 3.0.Ridden
What happens if they're shaking it precisely on a particular axis?Dictator
The best answer, because the iOS 3.0 motionBegan and motionEnded events are not very precise or accurate in terms of detecting the exact start and end of a shake. This approach allows you to be as precise as you want.Touching
UIAccelerometerDelegate accelerometer:didAccelerate: has been deprecated in iOS5.Houseless
This other answer is better and faster to implement https://mcmap.net/q/93051/-how-do-i-detect-when-someone-shakes-an-iphoneEdris
@Neron how to achieve vigorous shake with the above code.. I am really strugling hereKus
@millenomi, How can i detect the device movement just like as panorama?Towardly
How to identify shake if iphone is moved in X direction only? I don't want to detect shake if in Y or Z direction.Hutto
E
154

I finally made it work using code examples from this Undo/Redo Manager Tutorial.
This is exactly what you need to do:

  • Set the applicationSupportsShakeToEdit property in the App's Delegate:
  • 
        - (void)applicationDidFinishLaunching:(UIApplication *)application {
    
            application.applicationSupportsShakeToEdit = YES;
    
            [window addSubview:viewController.view];
            [window makeKeyAndVisible];
    }
    

  • Add/Override canBecomeFirstResponder, viewDidAppear: and viewWillDisappear: methods in your View Controller:
  • 
    -(BOOL)canBecomeFirstResponder {
        return YES;
    }
    
    -(void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        [self becomeFirstResponder];
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [self resignFirstResponder];
        [super viewWillDisappear:animated];
    }
    

  • Add the motionEnded method to your View Controller:
  • 
    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
        if (motion == UIEventSubtypeMotionShake)
        {
            // your code
        }
    }
    
    Energumen answered 8/3, 2010 at 23:58 Comment(4)
    Just to add to Eran Talmor solution : you should use a navigationcontroller instead of a viewcontroller in case of you've choose such an application in new project chooser.Tafilelt
    I tried using this but sometimes heavyshake or light shake it just stoppedKus
    Step 1 is now redundant, as the default value of applicationSupportsShakeToEdit is YES.Airlie
    Why call [self resignFirstResponder]; ahead of [super viewWillDisappear:animated];? This seems peculiar.Ushijima
    J
    94

    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 could alert various parts of the app when a shake occurred. Here's what I ended up doing.

    First, I subclassed UIWindow. This is easy peasy. Create a new class file with an interface such as MotionWindow : 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 from UIWindow to MotionWindow or whatever you called it. Save the xib. If you set up UIWindow programmatically, use your new Window class there instead.

    Now your app is using the specialized UIWindow class. 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 in viewWillAppear: and viewWillDisappear: 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 off of motionBegan vs. motionEnded. It's up to you. In my case, the effect always needs to take place after the device is at rest (vs. when it starts shaking), so I use motionEnded. Try both and see which one makes more sense ... or detect/notify 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 so for UIWindow subclasses? Is it because the window is at the top of the food chain?

    Johnathon answered 29/8, 2009 at 13:47 Comment(8)
    That's what's so weird. It works as-is. Hopefully it's not a case of "working in spite of itself" though! Please let me know what you find out in your own testing.Briefing
    I prefer this to subclassing a regular UIView, especially since you only need this to exist in one place, so putting in a window subclass seems a natural fit.Gynecoid
    Thank you jdandrea , i use your method but shaking getting multiple times !!! i just want users can shake one time (on a view that shaking does there ) ... ! how can i handle it ? i implement sth like this. i implement my code smht like this : i-phone.ir/forums/uploadedpictures/… ---- but the problem is app does shake only 1 time for life of application ! not only viewKnobloch
    Momeks: If you need the notification to occur once the device is at rest (post-shake), use motionEnded instead of motionBegan. That ought to do it!Briefing
    This is for sure the right way to do it. See Apples code example CLPaint.Michalmichalak
    @Joe D'Andrea - It works as-is because UIWindow is in the responder chain. If something higher up the chain intercepted and consumed these events, it would not receive them. developer.apple.com/library/ios/DOCUMENTATION/EventHandling/…Narration
    I really like this answer as a simple way to process shakes anywhere without fuss.Ridden
    This is great - but also, I'd probably call super, no?Corkwood
    B
    35

    I came across this post looking for a "shaking" implementation. millenomi's answer worked well for me, although i was looking for something that required a bit more "shaking action" to trigger. I've replaced to Boolean value with an int shakeCount. I also reimplemented the L0AccelerationIsShaking() method in Objective-C. You can tweak the ammount of shaking required by tweaking the ammount added to shakeCount. I'm not sure i've found the optimal values yet, but it seems to be working well so far. Hope this helps someone:

    - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
        if (self.lastAcceleration) {
            if ([self AccelerationIsShakingLast:self.lastAcceleration current:acceleration threshold:0.7] && shakeCount >= 9) {
                //Shaking here, DO stuff.
                shakeCount = 0;
            } else if ([self AccelerationIsShakingLast:self.lastAcceleration current:acceleration threshold:0.7]) {
                shakeCount = shakeCount + 5;
            }else if (![self AccelerationIsShakingLast:self.lastAcceleration current:acceleration threshold:0.2]) {
                if (shakeCount > 0) {
                    shakeCount--;
                }
            }
        }
        self.lastAcceleration = acceleration;
    }
    
    - (BOOL) AccelerationIsShakingLast:(UIAcceleration *)last current:(UIAcceleration *)current threshold:(double)threshold {
        double
        deltaX = fabs(last.x - current.x),
        deltaY = fabs(last.y - current.y),
        deltaZ = fabs(last.z - current.z);
    
        return
        (deltaX > threshold && deltaY > threshold) ||
        (deltaX > threshold && deltaZ > threshold) ||
        (deltaY > threshold && deltaZ > threshold);
    }
    

    PS: I've set the update interval to 1/15th of a second.

    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 15)];
    
    Blavatsky answered 10/11, 2008 at 17:14 Comment(2)
    How can i detect the device movement just like as panorama?Towardly
    How to identify shake if iphone is moved in X direction only? I don't want to detect shake if in Y or Z direction.Hutto
    M
    13

    In iOS 8.3 (perhaps earlier) with Swift, it's as simple as overriding the motionBegan or motionEnded methods in your view controller:

    class ViewController: UIViewController {
        override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent) {
            println("started shaking!")
        }
    
        override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
            println("ended shaking!")
        }
    }
    
    Millennial answered 20/4, 2015 at 22:39 Comment(3)
    Did you try it? I'd assume that to get this to work when the app is in the background would take a bit of extra legwork.Millennial
    Nope.. will soon.. but If you have noticed iPhone 6s it has this "pick up to wake screen" feature, where the screen brightens up for some time, when you pick up the device. I need to capture this behaviorGrandparent
    This should be the accepted answer. There's no need to bother with CoreMotion…Jink
    D
    12

    You need to check the accelerometer via accelerometer:didAccelerate: method which is part of the UIAccelerometerDelegate protocol and check whether the values go over a threshold for the amount of movement needed for a shake.

    There is decent sample code in the accelerometer:didAccelerate: method right at the bottom of AppController.m in the GLPaint example which is available on the iPhone developer site.

    Despair answered 29/9, 2008 at 21:17 Comment(0)
    R
    10

    This is the basic delegate code you need:

    #define kAccelerationThreshold      2.2
    
    #pragma mark -
    #pragma mark UIAccelerometerDelegate Methods
        - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 
        {   
            if (fabsf(acceleration.x) > kAccelerationThreshold || fabsf(acceleration.y) > kAccelerationThreshold || fabsf(acceleration.z) > kAccelerationThreshold) 
                [self myShakeMethodGoesHere];   
        }
    

    Also set the in the appropriate code in the Interface. i.e:

    @interface MyViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, UIAccelerometerDelegate>

    Retrorse answered 22/3, 2009 at 21:15 Comment(2)
    How can i detect the device movement just like as panorama?Towardly
    How to identify shake if iphone is moved in X direction only? I don't want to detect shake if in Y or Z direction.Hutto
    M
    7

    Check out the GLPaint example.

    http://developer.apple.com/library/ios/#samplecode/GLPaint/Introduction/Intro.html

    Metallophone answered 29/9, 2008 at 20:16 Comment(0)
    Y
    7

    Add Following methods in ViewController.m file, its working properly

        -(BOOL) canBecomeFirstResponder
        {
             /* Here, We want our view (not viewcontroller) as first responder 
             to receive shake event message  */
    
             return YES;
        }
    
        -(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
        {
                if(event.subtype==UIEventSubtypeMotionShake)
                {
                        // Code at shake event
    
                        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Motion" message:@"Phone Vibrate"delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                        [alert show];
                        [alert release];
    
                        [self.view setBackgroundColor:[UIColor redColor]];
                 }
        }
        - (void)viewDidAppear:(BOOL)animated
        {
                 [super viewDidAppear:animated];
                 [self becomeFirstResponder];  // View as first responder 
         }
    
    Yahwistic answered 15/10, 2012 at 12:11 Comment(0)
    S
    5

    Sorry to post this as an answer rather than a comment but as you can see I'm new to Stack Overflow and so I'm not yet reputable enough to post comments!

    Anyway I second @cire about making sure to set the first responder status once the view is part of the view hierarchy. So setting first responder status in your view controllers viewDidLoad method won't work for example. And if you're unsure as to whether it is working [view becomeFirstResponder] returns you a boolean that you can test.

    Another point: you can use a view controller to capture the shake event if you don't want to create a UIView subclass unnecessarily. I know it's not that much hassle but still the option is there. Just move the code snippets that Kendall put into the UIView subclass into your controller and send the becomeFirstResponder and resignFirstResponder messages to self instead of the UIView subclass.

    Squamous answered 18/8, 2009 at 15:5 Comment(3)
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.Mcripley
    @SashaSalauyou I clearly stated in the very first sentence here I didn't have enough reputation to post a comment at the timeSquamous
    sorry. It is possible here for 5-yo answer to hit into review queue ))Mcripley
    O
    5

    First off, I know this is an old post, but it is still relevant, and I found that the two highest voted answers did not detect the shake as early as possible. This is how to do it:

    1. Link CoreMotion to your project in the target's build phases: CoreMotion
    2. In your ViewController:

      - (BOOL)canBecomeFirstResponder {
          return YES;
      }
      
      - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
      {
          if (motion == UIEventSubtypeMotionShake) {
              // Shake detected.
          }
      }
      
    Onions answered 13/3, 2015 at 14:59 Comment(0)
    W
    4

    Easiest solution is to derive a new root window for your application:

    @implementation OMGWindow : UIWindow
    
    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
        if (event.type == UIEventTypeMotion && motion == UIEventSubtypeMotionShake) {
            // via notification or something   
        }
    }
    @end
    

    Then in your application delegate:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[OMGWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        //…
    }
    

    If you are using a Storyboard, this may be trickier, I don’t know the code you will need in the application delegate precisely.

    Whitacre answered 12/7, 2014 at 12:46 Comment(5)
    And yes, you can derive your base class in the @implementation since Xcode 5.Whitacre
    This works, I use it in several apps. So not sure why it has been voted down.Whitacre
    worked like a charm. In case you're wondering, you can override the window class like this: https://mcmap.net/q/94227/-subclassing-uiwindow-while-using-storyboardsHahnemann
    Will this work when app is in background? If not any other idea how to detect when in background?Grandparent
    This will probably work if you have a background-mode set.Whitacre
    A
    1

    Just use these three methods to do it

    - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    

    for details you may check a complete example code over there

    Akela answered 21/2, 2013 at 9:30 Comment(0)
    S
    1

    A swiftease version based on the very first answer!

    override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
        if ( event?.subtype == .motionShake )
        {
            print("stop shaking me!")
        }
    }
    
    Something answered 10/9, 2017 at 7:44 Comment(0)
    P
    0

    In swift 5, this is how you can capture motion and check

    override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
       if motion == .motionShake 
       {
          print("shaking")
       }
    }
    
    Prajna answered 25/1, 2021 at 5:53 Comment(0)
    R
    -1

    To enable this app-wide, I created a category on UIWindow:

    @implementation UIWindow (Utils)
    
    - (BOOL)canBecomeFirstResponder
    {
        return YES;
    }
    
    - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
        if (motion == UIEventSubtypeMotionShake) {
            // Do whatever you want here...
        }
    }
    
    @end
    
    Rhizome answered 15/9, 2017 at 19:45 Comment(0)

    © 2022 - 2024 — McMap. All rights reserved.