#7 編個有趣的,浪漫的,恐怖的, 感動的,心酸的,好笑的故事(歌詞)吧_冷笑話

Adam
彼得潘的 Swift iOS / Flutter App 開發教室
9 min readJul 30, 2024

學習目標:

  • 練習 IBAction & IBOutlet
  • 應用元件TextField、TextView、Button、ImageView
  • 使用 AVSpeechSynthesizer 講話
  1. 新增4個TextField,並在屬性欄Placeholder輸入要顯示的文字。

2. 新增ImageView和Button,ImageView圖片用內建的rectangle.fill,然後把兩個元件改成自己喜歡的顏色後,在右邊Identity介面做出圓角和陰影的效果,最後再把透明的TextView放在ImageView上面。

3. 再放上一隻貓圖案,畫面就大致完成了,接著就是拉 IBAction & IBOutlet 開始寫程式。

import UIKit
import AVFAudio

class ViewController: UIViewController {

@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var fatherNameTextField: UITextField!
@IBOutlet weak var sportTextField: UITextField!
@IBOutlet weak var drinkTextField: UITextField!
@IBOutlet weak var lameJokeTextView: UITextView!
let speaker = AVSpeechSynthesizer()

//鍵盤收起來的指令
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

//Gif動畫
func addGif(){
let snowImageView = UIImageView(frame: CGRect(x: 110, y: 550, width: 200, height: 200))
view.addSubview(snowImageView)
var images = [UIImage]()
for i in 0...4 {
images.append(UIImage(named: "animated-snow-image-0061-\(i)")!)
}
snowImageView.animationImages = images
snowImageView.animationDuration = 0.5
snowImageView.animationRepeatCount = 35
snowImageView.startAnimating()

}

//停止語音
func stopSpeak() {
speaker.stopSpeaking(at: .immediate)
}

@IBAction func speakLameJoke(_ sender: Any) {

if nameTextField.text == ""{
nameTextField.attributedPlaceholder = NSAttributedString(string: "請輸入主角的名稱", attributes: [NSAttributedString.Key.foregroundColor : UIColor.red])
}
else if fatherNameTextField.text == ""{
fatherNameTextField.attributedPlaceholder = NSAttributedString(string: "請輸入爸爸的名稱", attributes: [NSAttributedString.Key.foregroundColor : UIColor.red])
}
else if sportTextField.text == ""{
sportTextField.attributedPlaceholder = NSAttributedString(string: "請輸入做什麼運動", attributes: [NSAttributedString.Key.foregroundColor : UIColor.red])
}
else if drinkTextField.text == ""{
drinkTextField.attributedPlaceholder = NSAttributedString(string: "請輸入飲料", attributes: [NSAttributedString.Key.foregroundColor : UIColor.red])
}
else{
stopSpeak()
let jokeText = "有一天兒子\(nameTextField.text!)和爸爸\(fatherNameTextField.text!)一起在公園\(sportTextField.text!),結束後\(fatherNameTextField.text!)覺得很口渴,於是叫\(nameTextField.text!)去買\(drinkTextField.text!)給他喝,但\(nameTextField.text!)遲遲沒有去買,\(fatherNameTextField.text!)就生氣的說:『你是要逼爸渴死嗎?』,於是\(nameTextField.text!)就開始B-BOX了。"
lameJokeTextView.text = jokeText

//語音
let utterance = AVSpeechUtterance(string: jokeText)
utterance.volume = 0.5
utterance.rate = 0.5
utterance.pitchMultiplier = 1
utterance.voice = AVSpeechSynthesisVoice(language: "zh-TW")
speaker.speak(utterance)

addGif()
}

}

}

#Preview {
UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()!
}

因為我希望四個輸入框不要空白,所以在程式裡會先判斷是不是空字串,如果是空字串,placeholder 會顯示紅字提示。

4. 把顯示的內容顯示在TextView上,並用AVSpeechSynthesizer()說出來。

這邊為了避免連續點擊Button導致聲音重複,所以我都會先呼叫一次停止語音speaker.stopSpeaking(at: .immediate)。

--

--