撩妹燈謎(元宵猜燈謎 App)
Published in
6 min readJul 5, 2021
1.畫面截圖:
2.App操作GIF:
3.Github連結:
4.功能需求:
- 題庫有 n 題,隨機出其中的 10 題,每次玩的時候題目順序都不一樣。
2. 畫面上顯示目前題目是第幾題
3. 玩完後可選擇再玩一次,重新開始玩。
5.加分功能:
- 利用 AVSpeechSynthesizer 唸出題目和答案。
class SyntesizerModel: NSObject,ObservableObject,AVSpeechSynthesizerDelegate {
@Published var isSpeaking = false
@Published var isPaused = false
private var synthesizer = AVSpeechSynthesizer()
override init() {
super.init()
synthesizer.delegate = self
}
func speak(_ utterance: AVSpeechUtterance) {
if !isSpeaking && !isPaused{
self.synthesizer.speak(utterance)
}
else if isSpeaking && !isPaused {
self.synthesizer.pauseSpeaking(at: AVSpeechBoundary.immediate)
}
else if isPaused && !isSpeaking{
self.synthesizer.continueSpeaking()
}
}// MARK: AVSpeechSynthesizerDelegate
internal func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance) {
self.isSpeaking = true
}internal func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didPause utterance: AVSpeechUtterance) {
self.isSpeaking = false
self.isPaused = true
}internal func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didContinue utterance: AVSpeechUtterance) {
self.isSpeaking = true
self.isPaused = false
}
internal func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
self.isSpeaking = false
self.isPaused = false
}
}extension AVPlayer {static let correctPlayer: AVPlayer = {
guard let url = Bundle.main.url(forResource: "correct", withExtension:"mp3") else { fatalError("Failed to find sound file.") }
return AVPlayer(url: url)
}()static let incorrectPlayer: AVPlayer = {
guard let url = Bundle.main.url(forResource: "incorrect", withExtension:"mp3") else { fatalError("Failed to find sound file.") }
return AVPlayer(url: url)
}()
static let failurePlayer: AVPlayer = {
guard let url = Bundle.main.url(forResource: "failure", withExtension:"mov") else { fatalError("Failed to find sound file.") }
return AVPlayer(url: url)
}()
static let fairPlayer: AVPlayer = {
guard let url = Bundle.main.url(forResource: "fair", withExtension:"mp3") else { fatalError("Failed to find sound file.") }
return AVPlayer(url: url)
}()
static let winPlayer: AVPlayer = {
guard let url = Bundle.main.url(forResource: "win", withExtension:"mp3") else { fatalError("Failed to find sound file.") }
return AVPlayer(url: url)
}()func playFromStart() {
seek(to: .zero)
play()
}}
2. 做成選擇題版本,判斷是否答對和計分。