There is a need to be able to start a method regardless of which view controller the app is in and shakes the phone.
The idea is to use the Apple encapsulated "motionevent", but if you simply add a code to a view controller, then only when the view controller is displayed in the front-end, shaking the phone will not start the method.
-(BOOL) Canbecomefirstresponder {//default is no, so you have to override this method to set the Yes return yes;} Then implement the following methods://Much like the TouchEvent event-(void) Motionbegan: (uieventsubtype) Motion withevent: (Uievent *) event{}-(void) motionended: (Uieventsubtype) Motion withevent: (uievent *) event{ NSLog (@ "Shake");}-(void) motioncancelled: ( Uieventsubtype) Motion withevent: (Uievent *) event{}
Workaround: Expand the UIWindow with category and the code is as follows:
Uiwindow+.h
#import <UIKit/UIKit.h> #define Uieventsubtypemotionshakenotification @ " Uieventsubtypemotionshakenotification "@interface UIWindow (Motion)//@override-(BOOL) canbecomefirstresponder;-( void) motionended: (uieventsubtype) Motion withevent: (uievent *) event; @end
Uiwindow+.m
#import "Uiwindow+.h" @implementation UIWindow (Motion)-(BOOL) Canbecomefirstresponder {//default is no, so you have to override this method and set it to Yes return YES;} Then implement the following methods://Much like the TouchEvent event-(void) Motionbegan: (uieventsubtype) Motion withevent: (Uievent *) event{}-(void) motionended: (Uieventsubtype) Motion withevent: (uievent *) event{ NSLog (@ "Shake");}-(void) motioncancelled: ( Uieventsubtype) Motion withevent: (Uievent *) event{} @end