這個頁面主要的功能是:
為了達成這些功能,我們先建立一個協定,再實作整個控制器的邏輯。
我們使用 Delegate
讓 SoundViewController
能把「使用者選擇的音效」傳回給呼叫它的頁面:
protocol SoundViewControllerDelegate: AnyObject {
func didSelectSound(_ soundName: String)
}
接著在 SoundViewController 中定義需要的屬性
weak var delegate: SoundViewControllerDelegate?
let sounds = ["馬林巴琴", "鬼畜", "雷達", ]
var selectedSoundIndex: Int = 0
var audioPlayer: AVAudioPlayer?
var currentSelectedSound: String?
說明:delegate
:用來通知上一頁使用者的選擇。sounds
:音效名稱清單。selectedSoundIndex
:目前被選擇的音效位置,用來顯示打勾符號。audioPlayer
:播放音效的物件。currentSelectedSound
:進入此頁時的預設音效。
當頁面載入時,我們先設定 UI,並根據目前的音效名稱顯示正確的選取狀態。
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
if let currentSound = currentSelectedSound,
let index = sounds.firstIndex(of: currentSound) {
selectedSoundIndex = index
}
}
func setupUI() {
title = "提示聲"
tbvSound.register(UINib(nibName: "SoundTableViewCell", bundle: nil), forCellReuseIdentifier: "SoundTableViewCell")
tbvSound.delegate = self
tbvSound.dataSource = self
}
點擊音效後,頁面會立刻播放出選擇的聲音。
為了達成這點,我們使用 AVAudioPlayer:
// 播放音效
func playSound(named name: String) {
guard let url = Bundle.main.url(forResource: name, withExtension: "mp3") else { return }
do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer?.play()
} catch {
print("無法播放音效: \(error.localizedDescription)")
}
}
}
Tips:音效檔需事先加入專案(例如:馬林巴琴.mp3)。
最後,我們讓 SoundViewController 實作表格代理與資料來源:
extension SoundViewController: UITableViewDelegate, UITableViewDataSource {
// 返回音效數量
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sounds.count
}
// 返回每個音效的cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SoundTableViewCell", for: indexPath) as! SoundTableViewCell
cell.lbSound.text = sounds[indexPath.row]
cell.accessoryType = indexPath.row == selectedSoundIndex ? .checkmark : .none
return cell
}
// 當選擇某個音效時,更新選擇的音效並播放
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedSoundIndex = indexPath.row
delegate?.didSelectSound(sounds[selectedSoundIndex])
tableView.reloadData()
playSound(named: sounds[selectedSoundIndex])
}
}
這樣我們今天就成功把聲音的功能給完成了喔。