[英]Sequential sound upon dragging
I've built a small Xylophone app for iPhone, which has 7 buttons, each playing its own note when touched. 我为iPhone构建了一个小型Xylophone应用程序,该应用程序有7个按钮,每个按钮在触摸时都会播放自己的音符。 I want to expand its functionality, so that when a user drags their finger from top button to bottom most or other way round, all notes are played sequentially, just as they would if you dragged your finger across piano keys.
我想扩展其功能,以便当用户将手指从顶部按钮拖至最底部或以其他方式翻转时,所有音符将按顺序播放,就像在钢琴键上拖动手指一样。
I've experimented with adding various drag gestures (inside/outside, enter/exit), but they all work with only first button that receives an action. 我已经尝试过添加各种拖动手势(内部/外部,进入/退出),但是它们都只能通过第一个按钮来接收动作。 Ie the first sound is played when a finger is dragged outside of the first button, but when it enters the second button then nothing happens.
即,将手指拖到第一个按钮之外时播放第一个声音,但是当它进入第二个按钮时则什么也没有发生。
I was trying to connect different gestures (Touch Up Inside + Drag) into this single IBAction: 我试图将不同的手势(Touch + Inside + Drag)连接到这个IBAction中:
@IBAction func notePressed(_ sender: UIButton) {
playNote(noteNumber: sender.tag)
}
func playNote(noteNumber : Int) {
let soundURL = Bundle.main.url(forResource: "note\(noteNumber)", withExtension: "wav")
do {
try audioPlayer = AVAudioPlayer.init(contentsOf: soundURL!)
} catch {
print(error)
}
audioPlayer.play()
}
Maybe separate IBActions should be created? 也许应该创建单独的IBAction? Can anyone give me a hint on where I should be looking?
谁能给我提示我应该去哪里?
Here how I worked it around: 这是我的解决方法:
ViewController
class. ViewController
类。 Check the panView.swift
file. panView.swift
文件。 Here you'll create the event catcher to listen for the Swipe Gesture Recognizer
(That you need to add previously into your main storyboard) Swipe Gesture Recognizer
(您需要Swipe Gesture Recognizer
其添加到主故事板中) AVAudioPlaye
objects. AVAudioPlaye
对象数组。 Initialize those objects with the path URL
and then se them ready to play. URL
初始化这些对象,然后将其准备好播放。 See the prepareSounds()
function. prepareSounds()
函数。 More detailed explanation here 这里有更详细的解释
Let me know if you have any trouble recreating the app on your end. 如果您在重新创建应用程序时遇到任何麻烦,请告诉我。
Cheer! 欢呼!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.