作業7-2:App 的解鎖畫面(passcode)

Baaro
彼得潘的 Swift iOS / Flutter App 開發教室
5 min readJul 25, 2020
Photo by Dan Nelson on Unsplash

更新button 用過多的action

鍵盤鎖

整體配置為上圖所示。

上方5個image view 拉outlet

下面12個Button 拉 Action

//設定全域變數
var passwordStr = "1234" //預設密碼
var inputPasscodeStr = "" //預設輸入密碼

這裡使用字串的方式來做。

然後寫幾個function來處理。

inputPasscode()

給各個數字按鈕輸入的功能,

if 0...3 ~= inputPasscodeStr.count{
inputPasscodeStr += input
}

如果inputPasscodeStr 的個數 介於 0 ~ 3之間,

則將改數字加入inputPasscodeStr之中。

if inputPasscodeStr.count == 4 {
if inputPasscodeStr == passwordStr{
checkTrue()
}else{
reload()
checkFalse()
}
}

當輸入到第4個數的時候,

檢查inputPasscodeStr 是不是等於 passwordStr。

顯示正確或錯誤的function ,

換圖片、改背景色。

執行那4個圖片的顯示方式,

showImage()//--------////取得個數
let passcodeCount = inputPasscodeStr.count
//set image
let imageEmputy = UIImage(named: "baaro-inWhite")
let imageHave = UIImage(named: "baaro")

switch 判斷 passcodeCount 個數去執行更換的圖片。

這裏我全部設定了,不知道還有沒有更好的方式。

reload()//--------//func reload(){
//字串清空
inputPasscodeStr = ""
//reset image為預設樣式
checkImageSetDefault()
//清空後 重新載入4朵雲的設定
showImage()
}
func checkImageSetDefault(){
let imageDefault = UIImage(systemName: "questionmark")
checkImage.image = imageDefault
checkImage.backgroundColor = UIColor.systemGray
}

Action

@IBAction func btnDelete(_ sender: UIButton) {
if 1...3 ~= inputPasscodeStr.count{
inputPasscodeStr.removeLast()
}
showImage()
}

inputPasscodeStr.removeLast()

會刪除字串inputPasscodeStr 的最後一個字。

--

--