iOS Dev Detection device shake

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.