#42 運勢占卜APP

選一個問題,讓APP幫你占卜,心誠則靈,結果僅供參考。(#hw14–3)(#hw14–14)

操作影片

螢幕截圖

練習:

APP 的設計

流程蠻簡易的!

1. 先從 github 讀取自定義 JSON API,取得占卜題目

2. 點水晶球後,到 yesno 網站讀取 JSON API,取得占卜結果

3. 使用 kingfisher 抓取圖片

4. 顯示占卜結果

在 Github 準備好占卜的問題

從 github 讀取自定義 JSON API,取得占卜題目

https://raw.githubusercontent.com/JasonHungApp/JSON_API/main/YesOrNoQuestion

按下水晶球後開始占卜

在讀取API時,先簡單用個 UIActivityIndicatorView 讓畫面有動作,等資料跟 gif 圖片下載回來。

到 yesno 網站讀取 JSON API,取得占卜結果

https://yesno.wtf/api

取得 gif 網址後,用 kingfisher 去抓取圖片

self.imageView.kf.setImage(with: URL(string: yesNoResponse.image), completionHandler: { result in
switch result {
case .success(_):
DispatchQueue.main.async {
self.activityIndicator.stopAnimating() // 停止指示器
self.yesNoText = yesNoResponse.answer
self.showResultVC()
}

case .failure(let error):
print("載入圖片失敗:", error.localizedDescription)
}
})

圖片載入完成後,把參數傳到下一頁顯示。

1. 占卜的問題

2. 占卜的結果,YES / NO

3. YES 的 gif 檔 / NO 的 gif 檔

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "resultSegueIdentifier" {
if let resultVC = segue.destination as? resultViewController {

// 在這裡傳遞任何需要的資料到 resultViewController
let selectedRow = questionPickerView.selectedRow(inComponent: 0)
resultVC.question = questions[selectedRow]
resultVC.yesNoText = yesNoText.uppercased()
resultVC.image = imageView.image!
}
}
}

--

--