How to detect shake motion in iPhone

孤街醉人 提交于 2019-11-27 17:38:40

问题


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


回答1:


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




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




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/6042420/how-to-detect-shake-motion-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!