There are two methods for the current program to detect mobile phone shaking. The first method is to override the gesture method in the UIResponder class (which can be simulated by a simulator ), the second method is to use Accelerometer to check whether the mobile phone is shaking (you can only test it on a real machine ).
Method 1:
It takes only three steps: let the current ViewController support shaking gestures, make the current View the first responder, and rewrite the three methods in UIResponder that support shaking.
1. Shake the current ViewController;
Sample code:
-(Void) viewDidLoad
{
[SuperviewDidLoad];
// Supports shake
[[UIApplicationsharedApplication] setApplicationSupportsShakeToEdit: YES];
}
2. Make the current View the first responder;
Sample code:
-(BOOL) canBecomeFirstResponder
{// The default value is NO.
Return YES;
}
-(Void) viewDidAppear :( BOOL) animated
{
[SuperviewDidAppear: animated];
[SelfbecomeFirstResponder];
}
-(Void) viewWillDisappear :( BOOL) animated
{
[SelfresignFirstResponder];
[SelfviewWillDisappear: animated];
}
3. Rewrite the following method and add the code to be executed when the mobile phone is shaking in the corresponding method.
-(Void) motionBegan :( UIEventSubtype) motion withEvent :( UIEvent *) event // start shaking
-(Void) motionEnded :( UIEventSubtype) motion withEvent :( UIEvent *) event // shake ends
-(Void) motionCancelled :( UIEventSubtype) motion withEvent :( UIEvent *) event // cancel shaking
Method 2:
In fact, judging whether the mobile phone is shaking Based on acceleration can be divided into two types, which can also be considered as one.
Let's talk less about the Code directly:
1> use the UIAccelerometer object to check whether the mobile phone is shaking (iOS5.0 is not recommended in the future, but it can still be used, but there will be a warning)
// Create a UIAccelerometer instance
UIAccelerometer * accelerometer = [UIAccelerometersharedAccelerometer];
Accelerometer. updateInterval = 0.1;
Accelerometer. delegate = self;
// Proxy method for implementing the UIAccelerometer instance
-(Void) accelerometer :( UIAccelerometer *) accelerometer didAccelerate :( UIAcceleration *) acceleration
{// Not recommended after iOS5.0
If (fabs (acceleration. x)> 2.0 | fabs (acceleration. y)> 2.0 | fabs (acceleration. z)> 2.0 ){
NSLog (@ "shaking detected ");
}
}
2> use the CoreMotion framework to determine whether the mobile phone is shaking Based on the acceleration value. (The CoreMotion framework is recommended for developers by Apple to replace UIAccelerometer. I will paste a link to introduce the CoreMotion framework, and I feel like it is good)
Use CMMotionManager to obtain information such as accelerometer, gyro, and deviceMotion. There are two methods: push and pull. You can find the advantages and disadvantages of the two methods online.
1. Get the acceleration data in push mode. The following code can be put in the-(void) viewDidLoad method.
CMMotionManager * cmManager = [CMMotionManageralloc] init];
If (! CmManager. accelerometerAvailable ){
NSLog (@ "CMMotionManager unavailable ");
}
CmManager. accelerometerUpdateInterval = 0.1; // Data Update Interval
[CmManager startAccelerometerUpdatesToQueue: [NSOperationQueuecurrentQueue] withHandler: ^ (CMAccelerometerData * accelerometerData, NSError * error ){
Double x = accelerometerData. acceleration. x;
Double y = accelerometerData. acceleration. y;
Double z = accelerometerData. acceleration. z;
If (fabs (x)> 2.0 | fabs (y)> 2.0 | fabs (z)> 2.0 ){
NSLog (@ "shaking detected ");
}
NSLog (@ "CoreMotionManager, x: % f, y: % f, z: % f", x, y, z );
}];
2. If you use the pull method to obtain acceletation data, you must implement the timer function by yourself. The following code can be put in the-(void) viewDidLoad method.
CMMotionManager * cmManager = [CMMotionManageralloc] init];
If (! CmManager. accelerometerAvailable ){
NSLog (@ "CMMotionManager unavailable ");
}
CmManager. accelerometerUpdateInterval = 0.1f;
[CmManager startAccelerometerUpdates];
Timer = [NSTimerscheduledTimerWithTimeInterval: 0.2 ftarget: selfselector: @ selector (updateAcceleration :) userInfo: @ "123" repeats: YES];
[Timerfire];
Custom method updateAcceleration:
-(Void) updateAcceleration :( id) userInfo
{
CMAccelerometerData * accelData = cmManager. accelerometerData;
Double x = accelData. acceleration. x;
Double y = accelData. acceleration. y;
Double z = accelData. acceleration. z;
If (fabs (x)> 2.0 | fabs (y)> 2.0 | fabs (z)> 2.0 ){
NSLog (@ "shaking detected ");
}
NSLog (@ "CoreMotionManager, x: % f, y: % f, z: % f; userInfo: % @", x, y, z, (NSString *) userInfo );
}
Make sure to call the stopAccelerometerUpdates method ([cmManager stopAccelerometerUpdates]) of the CoreMotion instance to stop CoreMotion instance update regardless of the push or pull method.
OK. You can shake the phone and test it. ^_^...
Introduction to CoreMotion good article: http://www.cocoachina.com/iphonedev/sdk/2010/0811/1996.html