gpt4 book ai didi

ios - 如何将音频添加到 TableView 中的 UIButtons?

转载 作者:行者123 更新时间:2023-11-28 07:46:25 25 4
gpt4 key购买 nike

我一直在尝试使用 Swift 弄清楚如何将单独的音频文件添加到每个单元格中的每个 UIButton。我只能使一个音频文件工作,但该文件将自身附加到每个单元格中的每个按钮。这是我的代码:

import UIKit
import AVFoundation

public extension UITableView {
func indexPathForView(_ view: UIView) -> IndexPath? {
let origin = view.bounds.origin
let viewOrigin = self.convert(origin, from: view)
let indexPath = self.indexPathForRow(at: viewOrigin)
return indexPath
}
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, AVAudioPlayerDelegate {
var audioPlayer: AVAudioPlayer!

let elements = ["horse", "cat", "dog", "potato", "horse", "cat", "dog", "potato", "horse", "cat", "dog", "potato"]
let animalSounds = ["horse1", "cat1", "dog1", "potato1", "horse2", "cat2", "dog2", "potato2", "horse3", "cat3", "dog3", "potato3"]

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
tableView.delegate = self
tableView.dataSource = self

super.viewDidLoad()
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return elements.count
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! UITableTableViewCell

cell.cellView.layer.cornerRadius = cell.cellView.frame.height / 2

cell.animalLabel.text = elements[indexPath.row]
cell.animalImage.image = UIImage(named: elements[indexPath.row])
cell.animalImage.layer.cornerRadius = cell.animalImage.frame.height / 2
cell.animalImage.clipsToBounds = true
cell.playAnimalSound.actions(forTarget: "playAnimalNoises", forControlEvent: UIControlEvents.allEditingEvents)
cell.playAnimalNoises.button
return cell
}

@IBAction func playAnimalNoises(_ sender: UIButton) {
//let indexPath = self.tableView.indexPathForView(sender) //worked as sender
//playSounds(soundFileName: animalSounds)
print("Button pressed @ ")
}

func playSounds(soundFileName: String){
let soundURL = Bundle.main.url(forResource: soundFileName, withExtension: "wav")
do {
try audioPlayer = AVAudioPlayer(contentsOf: soundURL!)
}
catch {
print("Somethings wrong...")
}
audioPlayer.play()
}
}

// button shown in each cell,

如果您需要我进一步解释,请告诉我。

最佳答案

首先,您需要向单元格的按钮正确添加目标/操作。

删除:

cell.playAnimalSound.actions(forTarget: "playAnimalNoises", forControlEvent: UIControlEvents.allEditingEvents)
cell.playAnimalNoises.button

并添加:

cell.playAnimalSound.addTarget(self, action: #selector(playAnimalNoises), for: .touchUpInside)

然后更新playAnimalNoises:

@objc func playAnimalNoises(_ sender: UIButton) {
let point = self.tableView.convert(sender.bounds.origin, from: sender)
let indexPath = self.tableView.indexPathForRow(at: point)
playSounds(soundFileName: animalSounds[indexPath.row])
}

关于ios - 如何将音频添加到 TableView 中的 UIButtons?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50863574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com