Monday, January 3, 2011

Detecting a Slow Shake Vs. Fast Shake

Personally I have tried out the - (void)motionEnded...method and found the response time to be rather slow.

What I am trying to do is detect a fast shake vs. a slow shake and write a message. The project compiles, and if you shake below the 1.5 acceleration threshold I setup the labels says "Slow" as it is suppose to. Speed up the shake and it shows "Fast." THE PROBLEM is if I slow the shake back down the label doesn't change. What I have is as follows:

- (void)accelerometer : (UIAccelerometer *)accelerometer
didAccelerate : (UIAcceleration *)acceleration {

static NSInteger shakeCount = 0;
static NSDate *shakeStart;

NSDate *now = [[NSDate alloc] init];
NSDate *checkDate = [[NSDate alloc] initWithTimeInterval: 2.0f sinceDate:shakeStart];

if ([now compare:checkDate]== NSOrderedDescending || shakeStart == nil) {

shakeCount = 0;
[shakeStart release];
shakeStart = [[NSDate alloc] init];
}

[now release];
[checkDate release];

// Shake is too fast
if (acceleration.y > AccelerationThreshold) {
shakeCount++;

if (shakeCount > 4) {
myLabel.text = [NSString stringWithFormat:@"To Fast %i", shakeCount];
NSLog(@"To Fast");
shakeCount = 0;
shakeStart == nil;
[shakeStart release];
shakeStart = [[NSDate alloc] init];
// shakeCheckComplete = YES;
}

}



// Shake is slow
if (acceleration.y < AccelerationThreshold && acceleration.y > 0.5) {

shakeCount++;

if (shakeCount > 5) {
myLabel.text = [NSString stringWithFormat:@"Slow %i", shakeCount];
NSLog(@"Slow");
shakeCount = 0;
shakeStart == nil;
[shakeStart release];
shakeStart = [[NSDate alloc] init];
}
}




}

No comments:

Post a Comment

Followers