果粉測驗 檢測你是真假果粉? 選擇題App && Launch Screen Animation &&Progress Bar 使用

成果展示

使用者回答關於Apple公司的問題,根據分數判斷果粉程度

這次還有增加Launch Screen的動畫 &&使用看看Progress

動畫製作

補充在這篇

概念 DispatchQueue.main.asyncAfter 0.2s 後execute(執行)動畫

並轉換到下個VC

選澤題製作流程(第一頁)

所使用的變數

1.Quiz model 建立

建立新的swift.file 內quiz使用struct型別

設定struct命名Quiz,Quiz有題目、選項、正確答案

2.實例化Quiz 存入Array

3.取亂數不重複

var QuizNumberarray=[0,1,2,3,4,5,6,7,8,9,10,11,12,13] //題目總數array=QuizNumberarray.shuffled() 使用Shuffled() 使亂數不重複取 ex:
print(array=QuizNumberarray.shuffled())
Result - [6, 10, 7, 1, 11, 12, 13, 5, 2, 0, 4, 8, 3, 9]

4.設定button(選項)上的title作答並在後換題

func ChangeQuiz(){QuizLabel.text=Quizs[array[NumberQuiz]].QuizOption[0].setTitle(Quizs[array[NumberQuiz]].choice[0], for: .normal)Option[1].setTitle(Quizs[array[NumberQuiz]].choice[1], for: .normal)Option[2].setTitle(Quizs[array[NumberQuiz]].choice[2], for: .normal)Option[3].setTitle(Quizs[array[NumberQuiz]].choice[3], for: .normal)ScoreLabel.text="得分:\(score)"ProgressView.progress=TestProgress //設定Progress的值}

補充:多個button處理

5.按下Button 檢查答案記錄分數

比對sender.currtTitle 紀錄正確與否並增加分數

6.傳值下一頁顯示

實例化controller 在ResultViewController 設計變數score

對接上方紀錄的score 達成傳值

第二頁ResultVC

將圖片存入Array 使用if 判斷其Score 分級接著顯示對應的圖片

計時器

可以參考之前這篇寫的計時器

透過Timer來設定固定時間執行function 設定固定 0.1 秒執行 runTimer

timer=Timer.scheduledTimer(timeInterval: 0.1, target: self, selector:#selector(runTimer) , userInfo: nil, repeats: true)

func runTimer() 用counter 紀錄時間 並顯示在label上

@objc func runTimer(){counter+=0.1timeLabel.text = String(format: "%.1f", counter)
}

參考文件

程式碼

希望這篇文章能夠幫助到你(妳)

如有錯誤指正

I hope you found this guide helpful. If not, then please let me know either in the comments below, I’m AlberLee

Swift#12

--

--