[英]add sound to swipe gesture recognizer
I'm trying to make a sound play whenever a user swipes forward or back. 每当用户向前或向后滑动时,我都在尝试播放声音。
I've already added sounds to all of my UIButton
s with IBAction
s that play the sounds when the buttons are pressed, and it works no problem. 我已经使用
IBAction
将声音添加到我的所有UIButton
,它们在按下按钮时播放声音,并且没有问题。 But, when I try to do the same thing with a swipe gesture, it doesn't work. 但是,当我尝试用滑动手势执行相同的操作时,它不起作用。 The swipe gesture is created in the storyboard and the swiping works fine, but when I try to hook the gesture recognizer up to an IBAction that specifies that the sound should play, it still swipes but there's no sound.
在情节提要中创建了滑动手势,并且滑动操作正常,但是当我尝试将手势识别器挂接到指定应播放声音的IBAction上时,它仍会滑动但没有声音。 Is there a different way that I should be implementing this instead of with IBActions?
我应该使用IBActions来实现此方法吗?
edit: Upon further investigation it looks like the method that I'm hooking up to the swipe gesture recognizer is never getting called, despite it saying it's connected to it when I ctrl+click the gesture recognizer in the storyboard. 编辑:经过进一步调查,看来我连接到滑动手势识别器的方法从未被调用,尽管它说在我按住ctrl并单击情节提要中的手势识别器时已与之连接。 I have methods connected to my right swipes no problem, but I can't seem to get one to connect to the left swipe properly so that the method gets called.
我有连接到我的右滑动的方法没问题,但是似乎无法正确连接到左滑动,因此该方法被调用。
Try playing the sound either on the main thread, or after a delay. 尝试在主线程上或延迟播放声音。 Trying to play a sound while on an event thread might be causing some issues.
在事件线程上尝试播放声音可能会导致一些问题。 Try one code A or B and let me know what happens:
尝试一个代码A或B,让我知道会发生什么:
Code A: 代码A:
- (void)playAudioOnSwipe {
// Play the audio here
}
- (IBAction) onSwipe {
[self performSelector:@selector(playAudioOnSwipe) withObject:nil afterDelay:0.5];
}
Code B: 代码B:
- (void)playAudioOnSwipe {
// Play the audio here
}
- (IBAction) onSwipe {
[self performSelectorOnMainThread:@selector(playAudioOnSwipe) withObject:nil waitUntilDone:NO];
}
I never managed to figure out why the method attached to the swipe wasn't getting called but I just made it so that the desired sound was called in viewDidLoad of the page being swiped to; 我从未设法弄清为什么没有调用与滑动相关的方法,但是我只是这么做了,以便在要滑动到的页面的viewDidLoad中调用所需的声音。 not a perfect solution but it gets the job done and it sounds just fine.
不是一个完美的解决方案,但是它可以完成工作,而且听起来还不错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.