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. 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. Is there a different way that I should be implementing this instead of with 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. 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:
Code A:
- (void)playAudioOnSwipe {
// Play the audio here
}
- (IBAction) onSwipe {
[self performSelector:@selector(playAudioOnSwipe) withObject:nil afterDelay:0.5];
}
Code 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; not a perfect solution but it gets the job done and it sounds just fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.