How to detect shake motion in iPhone
Asked Answered
K

4

14

On the shake of the iPhone device i want some function to be called, i dont know how to recognize shake so i tried some link and tried this code

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if(event.type == UIEventSubtypeMotionShake)
    {
        NSLog(@"called");
        [self.view setBackgroundColor:[UIColor greenColor]];
    }
}

- (BOOL)canBecomeFirstResponder
{ 
return YES; 
}

But alas no luck so can you please let me know how i can do the same

Thanks and Regards

Kannan answered 18/5, 2011 at 9:19 Comment(6)
Where have you tried that code?Words
in the uiview controller subclass fileKannan
@Radix, I used that in UIWindow subclass and used NSNotification to notifity other controllers on shake event. Using that code in UICOntroller subclass didn't work for me as well if I remember correctlyWords
@Words : hey guess what i got that code working all i needed to do was to add this piece of code - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self becomeFirstResponder]; } its because the UIResponder shake event respond to the first responder as far as i have read in the documetation and got this thing working....Kannan
@Radix, hm, good point. Post that solution as an answer for your question and accept it - so others will be able to find your solution more easilyWords
@Vladimir: sure will do that but i am not able to answer my own quest but i will surely do that man \m/Kannan
K
34

As per the above comments discussed i am answering my question so that others can read the solution that i have got

In the UIResponder class documentation it is said that the motion events will respond to the first responder only so what i did was add just a small function and that did the trick for me, so here's my solution

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if(event.type == UIEventSubtypeMotionShake)
    {
        NSLog(@"called");
        [self.view setBackgroundColor:[UIColor greenColor]];
    }
}

- (BOOL)canBecomeFirstResponder
{ 
return YES; 
}

Now still i was not able to detect any shake motion so all i had to do was to make my viewcontroller the first responder and for that here's the code that i used

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

and i was done

This was the solution that i came up with

Thanks and Regards

Kannan answered 19/5, 2011 at 9:20 Comment(1)
Its work when shake iPhone and its detect. But is there any method is continues detect the shake ?Armyworm
H
2

You could do something like this...

Firstly...

Set the applicationSupportsShakeToEdit property in the App's Delegate:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    application.applicationSupportsShakeToEdit = YES;

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}

Secondly...

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];
 }

Thirdly...

Add the motionEnded method to your View Controller:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
   if (motion == UIEventSubtypeMotionShake)
   {
    // your code
   }
 }

That should work if the first answer did not and this is only quickly typed not tested:)

Hampton answered 1/4, 2013 at 22:59 Comment(0)
L
0

To globally handle shake across all screens of your app, you can subclass UIWindow and implement motionEnded, since the event will bubble up the responder chain until it reaches the window-object. Example using Xamarin:

public class MyWindow : UIWindow
{
    public MyWindow(CoreGraphics.CGRect frame)
        : base (frame)
    { }

    public override void MotionEnded (UIEventSubtype motion, UIEvent evt)
    {
        if (motion == UIEventSubtype.MotionShake) {
            Console.WriteLine ("Shake received");
        }
    }
}

Next, in AppDelegate, implement the window method to return an instance of your UIWindow subclass. Example using Xamarin:

public class AppDelegate : UIApplicationDelegate
{
    UIWindow _window;

    public override UIWindow Window 
    { 
        get
        {
            if (_window == null) {
                _window = new MyWindow (UIScreen.MainScreen.Bounds);
            }

            return _window;
        }
        set
        {
        }
    }

    // ...
}

Voilà, shake is handled across the whole app.

Lobbyism answered 30/8, 2015 at 16:32 Comment(0)
V
0

Things become easier with SSEventListener. Your view controller doesn't need to override motionEnded:withEvent: any more! With SSEventListener, you can listener for shake event as simple as this:

[viewController ss_setShakeEventListener:^{ ... }];

Virgate answered 15/1, 2018 at 6:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.