App 的解鎖畫面

🌟畫面包含:
1. 建立IBOutlet:

・[UIImageView] 表示當前密碼
・[UIButton] 數字按鍵

2. 建立IBAction:
・UIButton enterPasscode 輸入密碼
・UIButton backward 清除密碼一位數

1. 建立IBOutlet:

2. 建立IBAction:

func ImageChange()使輸入密碼時,同步顯示image,表示輸入至當前字數。及使用Switch替代if…else…,for迴圈設定imageView.isHighlighted。
當entercode字數為4時,要確認密碼是否正確。

func checkPasscode () 確認答案否正確。使用到UIAlertController設定訊息文字,UIAlertAction設定訊息動作。
func reset() 歸零重新(用於UIAlertAction裡)

UIButton enterPasscode 輸入密碼

因畫面未顯示密碼,因此可以加入print(entercode)檢視點選的btn密碼是否相對應。

@IBAction func enterPasscde(_ sender: UIButton) {
//當entercode的字數不等於4時
if entercode.count != 4 {
//輸入的數字為Btn的名稱
if let inputNumber = sender.currentTitle {
//entercode加入inputNumber
entercode.append(inputNumber)
}
}
//同步imageView
imageChange()
}

UIButton backward 清除密碼一位數

@IBAction func backward(_ sender: UIButton) {
//當entercode的字數不等於0時,entercode的數字dropLast(1)刪除當前最後一個數字
if entercode.count != 0 {
entercode = String(entercode.dropLast(1))
//同步ImageView
imageChange()
}
}

Reference

Github

--

--