I'm trying to implement a function that detects a user shaking his/her iPhone, and then the app will make a barking sound. So far I've got the code attached below, and it works. But, if I shake the phone harder, it makes 2 or more barks one immediately after the other. How do I make sure this only happens once per shake, no matter how hard?
- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration {
if (acceleration.x > kAccelerationThreshold ||
开发者_运维问答 acceleration.y > kAccelerationThreshold ||
acceleration.z > kAccelerationThreshold) {
// BARKING SOUND
}
}
You could use an NSTimer - look here: section "Timer"
After playing the sound, set a boolean variable called "hasBarked" to YES and call the timer then. After two seconds, the timer sets "hasBarked" to NO and disables itself. Finally, put the whole sound-playing method into an if block that only plays the sound if "hasBarked" is NO.
Why not keep a value around with the last time you played the sound?
time_t last_time_played_bark = 0;
#define MINIMUM_TIME_BETWEEN_BARKS 5 // in seconds
time_t now = time(NULL);
if (last_time_played_bark + MINIMUM_TIME_BETWEEN_BARKS < now) {
// BARKING_SOUND
last_time_played_bark = now;
}
Presumably you'll want MINIMUM_TIME_BETWEEN_BARKS to be at least as long as it takes to play the sound.
As a side note, if you just want to detect shake gestures, you might find it easier to use the motion events added in OS 3.0 than trying to roll your own from raw accelerometer data.
I agree with Epsilon Prime's suggestion to store the timestamp of the last time you played the sound as a means to throttle how often you play it.
精彩评论