. h Files
@interface shakeviewcontroller:uiviewcontroller<uiaccelerometerdelegate>
{
uiaccelerationvalue myaccelerometer[3];
Whether to respond to a shake of the flag
BOOL _canshake;
In the. m file
#define KFILTERINGFACTOR 0.1 #define KERASEACCELERATIONTHRESHOLD 2.0 @implementation Shakeviewcontr Oller-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil {self = [super Initwithnibname
: Nibnameornil Bundle:nibbundleornil];
if (self) {_canshake = YES;
return self;
}-(void) Dealloc {[Uiaccelerometer sharedaccelerometer].delegate = nil;
[Super Dealloc];
}-(void) viewdidload {[Super viewdidload];
[Uiaccelerometer sharedaccelerometer].delegate = self;
[Uiaccelerometer sharedaccelerometer].updateinterval = 1.0f/40.0f; } #pragma mark-uiaccelerometerdelegate-(void) Accelerometer: (Uiaccelerometer *) Accelerometer didaccelerate: (
Uiacceleration *) Acceleration {uiaccelerationvalue length, x, Y, Z;
if (!_canshake) {return; }//use a basic high-pass filter to remove the influence of the gravity myaccelerometer[0] = acceleration.x * Kfilteringfactor+ myaccelerometer[0] * (1.0-kfilteringfactor);
MYACCELEROMETER[1] = acceleration.y * kfilteringfactor + myaccelerometer[1] * (1.0-kfilteringfactor);
MYACCELEROMETER[2] = acceleration.z * kfilteringfactor + myaccelerometer[2] * (1.0-kfilteringfactor);
Compute values for the three axes of the acceleromater x = acceleration.x-myaccelerometer[0];
y = acceleration.y-myaccelerometer[0];
z = acceleration.z-myaccelerometer[0];
Compute the intensity of the current acceleration length = sqrt (x * x + y * y + z * z); If above a given threshold, play the erase sounds and erase the drawing view if (length >= Keraseaccelerationthre
Shold) {//whether response to shake the sign _canshake = NO;
[Self shakeevent]; }
}