开发者

How to detect shake motion in iPhone

开发者 https://www.devze.com 2023-03-07 03:44 出处:网络
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

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


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


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:)


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.


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:^{ ... }];

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号