There are two ways to detect shaking:
1. inherit from uiwindow
-(Void) motionbegan :( uieventsubtype) motion withevent :( uievent *) event {}- (void) motionended :( uieventsubtype) motion withevent :( uievent *) event {If (motion = uieventsubtypemotionshake) {// user was shaking the device. post a notification named "Shake ". [[nsicationcenter center defacenter center] postnotificationname: @ "Shake" Object: Self]; // message registration}-(void) motioncancelled :( uieventsubtype) motion withevent :( uievent *) event {}
2. In appdelegate. m
-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {... application. applicationsupportsshaketoedit = yes; // Add here... return yes ;}
Then, in viewcontroller
-(Bool) canbecomefirstresponder {return yes;}-(void) viewdidappear :( bool) animated {[Super viewdidappear: animated]; [self becomefirstresponder];}-(void) viewwilldisappear :( bool) animated {[self resignfirstresponder]; [Super viewwilldisappear: animated];}-(void) motionended :( uieventsubtype) motion withevent :( uievent *) event {If (motion = uieventsubtypemotionshake) {nslog (@ "Shake ");}}
Original article:
Http://ilewen.com/questions/778/%E7%A8%8B%E5%BA%8F%E6%80%8E%E4%B9%88%E6%A3%80%E6%B5%8B%E7%94%A8%E6%88%B7%E5%9C%A8%E6%91%87%E5%8A%A8iphone%EF%BC%9F