UIAccelerometer, in short, is to obtain the gravity sensing and displacement judgment and processing of the mobile phone. For example, shaking the mobile phone up or down will trigger the accelerator event. Today, we will learn how to use the accelerator.
1. Code. The implementation of the accelerator is very simple, as long as the only method in the proxy is implemented.
UIAccelerometerDelegate
The proxy method is
-(Void) accelerometer :( UIAccelerometer *) accelerometer didAccelerate :( UIAcceleration *) acceleration NS_DEPRECATED_IOS (2_0, 5_0)
Acceleration encapsulates the xyz coordinates of the accelerator. We can operate our Genie Based on xyz changes.
1-) the simplest assignment of method1
UIAccelerationValue x,y,z; x = acceleration.x; y = acceleration.y; z = acceleration.z;
We can perform corresponding processing based on the obtained xyz value. But this xyz value contains gravity, our acceleration event. Resolution later
2) obtain the part affected by gravity. Use the low-pass Filtering Algorithm
/// # Define kAccelerometerFrequency 50.0 // # define kFilteringFactor 0.1 // obtain the UIAccelerationValue accleX, accleY, accleZ; accleX = (acceleration. x * kFilteringFactor) + (accleX * (1.0-kFilteringFactor); accleY = (acceleration. y * kFilteringFactor) + (accleY * (1.0-kFilteringFactor); accleZ = (acceleration. z * kFilteringFactor) + (accleZ * (1.0-kFilteringFactor ));
3) Remove the parts affected by gravity from x to obtain the influence of the final xyz accelerator
// Filter the low-pass data out of the current value to obtain the simplified high-pass data UIAccelerationValue haccleX, haccleY, haccleZ; haccleX = x-accleX; haccleY = y-accleY; haccleZ = z-accleZ; // use your own method to process data self. isTouchEnabled = YES;
Note that we need to set a touch response.
Self. isTouchEnabled = YES;