tags:

views:

62

answers:

2

Hi, 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 ?

+1  A: 

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];
    }
}
WoLpH
excuse me ! i don;t understand can you clear for me ?
Momeks
@Momeks: I've added a very simple example of what I mean. Basically you just check when the last shake occured and ignore any shake within a given timeframe. It might be useful to combine it with @Marichka's code though. I don't have any experience with the iPhone accelerometer so I don't know how sensitive it is.
WoLpH
i explained the main problem here ! : http://stackoverflow.com/questions/3414800/shake-iphone-with-accelerometercan you take a look ?
Momeks
@Momekes: I'm not that well versed with the iPhone SDK, I can't really help you with that problem.
WoLpH
+1  A: 

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

Marichka