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

Radix
  • 3,639
  • 3
  • 31
  • 47
  • Where have you tried that code? – Vladimir May 18 '11 at 09:23
  • in the uiview controller subclass file – Radix May 18 '11 at 09:28
  • @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 correctly – Vladimir May 18 '11 at 09:31
  • @Vladimir : 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.... – Radix May 18 '11 at 10:12
  • @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 easily – Vladimir May 18 '11 at 10:22
  • @Vladimir: sure will do that but i am not able to answer my own quest but i will surely do that man \m/ – Radix May 18 '11 at 10:51

4 Answers4

33

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

Radix
  • 3,639
  • 3
  • 31
  • 47
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:)

Thomas Stone
  • 287
  • 3
  • 9
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.

Bjørn Egil
  • 2,398
  • 1
  • 20
  • 22
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:^{ ... }];