The two methods of device shaking detection are simply recorded under
Method One
First, add the delegate in the
-(BOOL) Application: (uiapplication *) application didfinishlaunchingwithoptions: ( Nsdictionary *) launchoptions
{
//Override point for customization after application launch
// Add detection shake
application. Applicationsupportsshaketoedit =YES;
}
Second, add in the Viewcontroller to be detected
// detect mobile phone shake
-(BOOL) Canbecomefirstresponder
{
return YES;
}
-(void) Viewwilldisappear: (BOOL) Animated {
[Self resignfirstresponder];
[Super viewwilldisappear:animated];
}
-(void) motionended: (uieventsubtype) Motion withevent: (uievent *) event
{
if (motion = = Uieventsubtypemotionshake)
{
Uialertview *alertview = [[Uialertview alloc]initwithtitle:@ ' hint ' message:@ ' congratulations on getting a 100-5 voucher "Delegate:self cancelbuttontitle:@" close "Otherbuttontitles:nil";
[Alertview show];
NSLog (@ " Shake detected ");
}
}
-(void) Prargotproblem: (NSString *) problemtitle withdetails: (NSString *) problemdetails
{
[Self alert:problemtitle withdetails:problemdetails];
}
Method two using Coremotion
Introduce the required header files
#import <CoreMotion/CoreMotion.h>
Viewdidload initialization required to be detected Cmmotionmanager simultaneously initiates a Nstimer to detect changes in the X, Y, Z axes
-(void) Viewdidload {
[superviewdidload];
Additional setup after loading the view.
nstimer *autotimer = [ nstimer scheduledtimerwithtimeinterval: 1.0/60.0 target: self selector : @selector (autochange) userinfo: nil repeats: yes ];
_manager = [[cmmotionmanageralloc]init];
_manager. Accelerometerupdateinterval=1.0/60.0;
[_managerstartaccelerometerupdates];
}
-(void) Autochange
{
//adjust x y z according to your own needs
if (FABSF(_manager.Accelerometerdata.Acceleration.X) >1.0 || FABSF(_manager.Accelerometerdata.Acceleration.Y) >1.2 || FABSF(_manager.Accelerometerdata.Acceleration.Z) >0.5)
{
NSLog(@ " I'm shaking ... ");
}
}
Note: Method one in the case of large shaking amplitude will be called, method two can be adjusted according to their own needs
iOS Dev Detection device shake