开发者

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 ?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜