iOS : How to detect Shake motion?

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

提交回复
热议问题