iOS : How to detect Shake motion?

前端 未结 4 993
甜味超标
甜味超标 2021-01-31 10:01

I added the following code to my appDelegate.m

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

- (void)motionEnded:(UIEventSubtype)mot         


        
相关标签:
4条回答
  • 2021-01-31 10:29

    This might help you...
    https://stackoverflow.com/a/2405692/796103

    He says that you should set the UIApplication's applicationSupportsShakeToEdit to YES. and override 3 methods in your VC:

    -(BOOL)canBecomeFirstResponder {
        return YES;
    }
    
    -(void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        [self becomeFirstResponder];
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [self resignFirstResponder];
        [super viewWillDisappear:animated];
    }
    

    The rest of your code is fine. (the -motionEnded:withEvent:)

    0 讨论(0)
  • 2021-01-31 10:30

    I extended UIApplication class and added class reference to main: MyApplication.h

    @interface MyApplication : UIApplication
    
    @end
    

    MyApplication.m

    @implementation MyApplication
    
    - (void) sendEvent:(UIEvent *)event
    {
        if( event && (event.subtype==UIEventSubtypeMotionShake))
        {
            AppDelegate *objAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
            [objAppDelegate doWhatEver];
            [super sendEvent:event];
        }
        else
        {
            [super sendEvent:event];
        }
    }
    
    @end
    

    And the last step in main.m

    int main(int argc, char *argv[])
    {
    return UIApplicationMain(argc, argv, NSStringFromClass([MyApplication class]), NSStringFromClass([AppDelegate class]));
    }
    

    This works in all cases.

    0 讨论(0)
  • 2021-01-31 10:38

    If you want to be able to detect the shake motion across the app, the best way is to override the class of your application with a custom class.

    And then implement this in your custom application class

    @implementation PSApplication
    
    - (void)sendEvent:(UIEvent *)event
    {
        if ( event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake ) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"shakeNotification" object:nil];
        }
    
        [super sendEvent:event];
    }
    
    @end
    
    0 讨论(0)
  • 2021-01-31 10:41

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

    0 讨论(0)
提交回复
热议问题