0

我正在编写一个应用程序,它不仅需要检测用户摇动一次,而且需要持续检测。这个想法是声音会在一次摇晃时播放一次,如果设备持续摇晃,声音会循环播放。

我已经使用 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");
    }
}
4

1 回答 1

0

看起来苹果有一个摇晃的手势。看看这是否有帮助:

http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MotionEvents/MotionEvents.html

于 2012-08-31T22:00:23.057 回答