IOS implementation Shake source code __ios

Source: Internet
Author: User

. 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]; }
}




Related Article

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.