我正在编写一个应用程序,它不仅需要检测一次用户抖动,而且需要连续检测。这个想法是,一次震动会播放一次声音,如果设备持续震动,声音会循环。
我已经用Shake API和Accelometer 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");
}
}
看起来苹果有一个摇晃的手势。看看这是否有帮助:
http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MotionEvents/MotionEvents.html