Mc.Lover
Mc.Lover

Reputation: 4994

How Prevent multiple times iPhone shaking?

i used this method to handle shake event on my app , but i have huge problem that drive me crazy !

i use this method : http://www.iphonedevsdk.com/forum/iphone-sdk-development/55087-iphone-shake-detection-problem.html#post230283

but the problem is app detect multiple times shake event !

2010-08-10 14:06:43.700 APP[406:207] SHAKED
2010-08-10 14:06:44.340 APP[406:207] SHAKED
2010-08-10 14:06:44.692 APP[406:207] SHAKED
2010-08-10 14:06:44.868 APP[406:207] SHAKED
2010-08-10 14:06:45.044 APP[406:207] SHAKED
2010-08-10 14:06:45.172 APP[406:207] SHAKED
2010-08-10 14:06:45.332 APP[406:207] SHAKED
2010-08-10 14:06:45.492 APP[406:207] SHAKED
2010-08-10 14:06:45.644 APP[406:207] SHAKED

how cab i handle it ?

Upvotes: 0

Views: 165

Answers (2)

Wolph
Wolph

Reputation: 80031

You have to debounce the data before it is usable. The principle is quite simple, once you read a SHAKE just ignore all values for the next second or so.

You might want to consider making the sensitivity (delay) configurable.

Example code:

NSDate *start = [NSDate date];

-(void)method:(UIAccelerometer *)accelerometer {
    NSTimeInterval timeInterval = [start timeIntervalSinceNow];
    /* if our last shake was more than 1 second ago */
    if(timeInterval > 1.0){
        /* do yourthing here */
        start = [NSDate date];
    }
}

Upvotes: 1

Marichka
Marichka

Reputation: 397

Probably the problem is that you detect every slight change of the accelerometer data. I use the following code to detect average shake:

- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 
{
    if(sqrt(acceleration.x*acceleration.x + acceleration.y*acceleration.y + acceleration.z*acceleration.z)>3.0)
    {
        [self DoSomething];
    }
}

You can change 3.0 value to detect stronger/lighter shakes

Upvotes: 1

Related Questions