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");
}
}
I am writing an app that will need to detect a users shake not only once but continuously. The idea is that a sound would play once on a single shake and the sound would loop if the device is continuously shaken.
I have tested it with the both the Shake API and Accelerometer API but neither do exactly what I want. Here is what I've got so far:
- (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");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来苹果有摇晃手势。看看这是否有帮助:
http://developer .apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MotionEvents/MotionEvents.html
It looks like Apple has a shake gesture. See if this helps:
http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MotionEvents/MotionEvents.html