iOS : How to detect Shake motion?

前端 未结 4 1077
甜味超标
甜味超标 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: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.

提交回复
热议问题