简体   繁体   English

拖动时发出连续声音

[英]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: 这是我的解决方法:

  • Expand the 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其添加到主故事板中)
  • Create an array of 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM