0
我正在編寫一個應用程序,它需要檢測用戶不僅一次而且連續地震動。這個想法是,一次振動就會播放一次聲音,如果設備不斷震動,則聲音會循環播放。iOS 5檢測重複/連續抖動
我已經用Shake API和Accelerometer API對它進行了測試,但都沒有做到我想要的。這是我到目前爲止:
- (void)playAudioFile
{
soundFile = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"boing" ofType:@"wav"]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
[audioPlayer setDelegate:self];
[audioPlayer play];
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
if (acceleration.x > 0.5 || acceleration.y > 0.5 || acceleration.z > 0.5) {
[self playAudioFile];
NSLog(@"Trigger @ 0.5x");
}
}
試用蘋果搖動以隨機播放功能,你會發現即使是不可靠地重現。有時需要劇烈的晃動,其他時候只需輕微的震動就足夠了。如果你有不同世代的多個iphones,這個差別就更明顯了。 我會想象你會遇到與你自己的實現相同的問題。 – 2012-01-16 22:17:44
請注意,從iOS 5開始不推薦使用'accelerometer:didAccelerate:'。 – Pascal 2012-08-19 14:47:16