App程式設計入門#Outlet & Action
Published in
3 min readAug 15, 2020
幾週前僅只是練習使用IOS SDK來說話,
本週是進階版!
學習利用程式中的Outlet & Action語法結合ViewController上的按鈕,
可以說出自行輸入的話語,可以調整速度等等。
#本週實作#
#程式碼#
import UIKitimport AVFoundationclass ViewController: UIViewController {@IBOutlet weak var loveTextField: UITextField!@IBOutlet weak var voiceTypeSlider: UISlider!@IBOutlet weak var speedRateSlider: UISlider!override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view.}@IBAction func speak(_ sender: Any) {let speechUtterance = AVSpeechUtterance(string:loveTextField.text!)speechUtterance.voice = AVSpeechSynthesisVoice(language: “zh-TW”)let synthesizer = AVSpeechSynthesizer()speechUtterance.pitchMultiplier = voiceTypeSlider.valuespeechUtterance.rate = speedRateSlider.valuesynthesizer.speak(speechUtterance)}}
#參考資料#