我的应用是队列号播音员。它会在UILabel
上得到数字,我正在寻找一种方法,这样我就可以从UILabel
导出数字并给它们条件,以便它播放每个数字的声音。这是我到目前为止所拥有的:
import UIKit
import AVFoundation
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
var Sound1:AVAudioPlayer?
var Sound2:AVAudioPlayer?
var Sound3:AVAudioPlayer?
var Sound4:AVAudioPlayer?
var Sound5:AVAudioPlayer?
var Sound6:AVAudioPlayer?
var Sound7:AVAudioPlayer?
var Sound8:AVAudioPlayer?
var Sound9:AVAudioPlayer?
var Sound10:AVAudioPlayer?
@IBOutlet weak var label: UILabel!
@IBOutlet weak var pickerView: UIPickerView!
let numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 3
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return numbers[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return numbers.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let val1 = numbers[pickerView.selectedRow(inComponent: 0)]
let val2 = numbers[pickerView.selectedRow(inComponent: 1)]
let val3 = numbers[pickerView.selectedRow(inComponent: 2)]
label.text = "\(val1) \(val2) \(val3)"
}
fileprivate func num(_ i: Int) -> Int {
return pickerView.selectedRow(inComponent: i)
}
@IBAction func buttonPressed() {
let currentNum = num(0) * 100 + num(1) * 10 + num(2)
let nextNum = currentNum + 1
pickerView.selectRow(nextNum % 1000 / 100, inComponent: 0, animated: true)
pickerView.selectRow(nextNum % 100 / 10, inComponent: 1, animated: true)
pickerView.selectRow(nextNum % 10, inComponent: 2, animated: true)
changeLabelText()
}
fileprivate func changeLabelText() {
label.text = "\(num(0)) \(num(1)) \(num(2))"
}
//Announcer Button Icon Setting
@IBOutlet weak var speaker: UIButton!
@IBAction func speakerButton(_ sender: UIButton) {
speaker.setImage(#imageLiteral(resourceName: "speakerOn"), for: .highlighted)
}
//Announcer Button
@IBAction func soundPlayed(_ sender: Any) {
accouncingNumbers()
}
//Sounds imported here
do
{
let audioURL1 = Bundle.main.url(forResource: "1", withExtension: "mp3")!
Sound1 = try AVAudioPlayer(contentsOf: audioURL1)
Sound1?.prepareToPlay()
let audioURL2 = Bundle.main.url(forResource: "2", withExtension: "mp3")!
Sound2 = try AVAudioPlayer(contentsOf: audioURL2)
Sound2?.prepareToPlay()
let audioURL3 = Bundle.main.url(forResource: "3", withExtension: "mp3")!
Sound3 = try AVAudioPlayer(contentsOf: audioURL3)
Sound3?.prepareToPlay()
let audioURL4 = Bundle.main.url(forResource: "4", withExtension: "mp3")!
Sound4 = try AVAudioPlayer(contentsOf: audioURL4)
Sound4?.prepareToPlay()
let audioURL5 = Bundle.main.url(forResource: "5", withExtension: "mp3")!
Sound5 = try AVAudioPlayer(contentsOf: audioURL5)
Sound5?.prepareToPlay()
let audioURL6 = Bundle.main.url(forResource: "6", withExtension: "mp3")!
Sound6 = try AVAudioPlayer(contentsOf: audioURL6)
Sound6?.prepareToPlay()
let audioURL7 = Bundle.main.url(forResource: "7", withExtension: "mp3")!
Sound7 = try AVAudioPlayer(contentsOf: audioURL7)
Sound7?.prepareToPlay()
let audioURL8 = Bundle.main.url(forResource: "8", withExtension: "mp3")!
Sound8 = try AVAudioPlayer(contentsOf: audioURL8)
Sound8?.prepareToPlay()
let audioURL9 = Bundle.main.url(forResource: "9", withExtension: "mp3")!
Sound9 = try AVAudioPlayer(contentsOf: audioURL9)
Sound9?.prepareToPlay()
let audioURL10 = Bundle.main.url(forResource: "10", withExtension: "mp3")!
Sound10 = try AVAudioPlayer(contentsOf: audioURL10)
Sound10?.prepareToPlay()
}
catch
{
print(error)
}
}
数字从1到999开始,UILabel
上的打印数字来自3位UIPickerView
。如何从UILabel
导入数字以使其处于某种状态,以便在用户点击UIButton
时播放正确的播音员数量?
答案 0 :(得分:1)
不要“从UILabel导入数字”。标签是 view 。数字是 model (数据)。您应该从不使用视图作为重要数据的唯一存储库。如果您以后需要这些数字,请事先将它们保存为数据,以便以后随时检索。