swift密碼輸入彈窗

首先附上鏈接:https://github.com/wangtaoHappy/SwiftPassword
在此 demo中主要涉及到的知識(shí)點(diǎn)有一下幾個(gè):
1:swift中協(xié)議的寫法和使用
聲明協(xié)議,繼承自NSObjectProtocol

protocol PasswordAlertViewDelegate:NSObjectProtocol{
    func passwordCompleteInAlertView(alertView:PasswordAlertView, password:NSString)
}

設(shè)定屬性

weak var delegate:PasswordAlertViewDelegate?

將想要得到的值傳遞出去

func sure(sender:UIButton) {
        print("確定")
        delegate?.passwordCompleteInAlertView(alertView:self, password: self.textFiled.text! as NSString)
    }

遵守協(xié)議

class ViewController: UIViewController,PasswordAlertViewDelegate

在UIController中的調(diào)用

func inputPassword() {
        let pswAlertView = PasswordAlertView.init(frame: self.view.bounds)
        pswAlertView.delegate = self
        self.view.addSubview(pswAlertView)
    }
   

    func passwordCompleteInAlertView(alertView: PasswordAlertView, password: NSString) {
        alertView.removeFromSuperview()
        print("password:",password);
        self.label.text = NSString.init(format: "密碼:\(password)" as NSString) as String
    }

2:鍵盤的出現(xiàn)和隱藏的監(jiān)聽,以及view隨鍵盤高度變化而變化
鍵盤監(jiān)聽

NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow(Info:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide(Info:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

得到信息后,根據(jù)鍵盤高度設(shè)置彈窗的高度

func keyBoardWillShow(Info:NSNotification) {
        let userInfos = Info.userInfo![UIKeyboardFrameEndUserInfoKey]
        
        let heigh = ((userInfos as AnyObject).cgRectValue.size.height)
        
        self.BGView.center = CGPoint.init(x: self.BGView.center.x , y:ScreenH - heigh - self.BGView.frame.size.height/2 - 10)
    }
    
    func keyBoardWillHide(Info:NSNotification) {
        self.BGView.center = self.center
    }

3:textFiled的使用方法

let textFiled = UITextField.init(frame: CGRect.init(x: 0, y: Int(line.frame.origin.y) + 30, width: Int(bgViewW), height: borderH))
        textFiled.backgroundColor = UIColor.clear
        //設(shè)置代理
        textFiled.delegate = self
        //監(jiān)聽編輯狀態(tài)的變化
        textFiled.addTarget(self, action: #selector(textFiledValueChanged(textFiled:)), for: .editingChanged)
        textFiled.tintColor = UIColor.clear
        textFiled.textColor = UIColor.clear
        textFiled.becomeFirstResponder()
        //設(shè)置鍵盤類型為數(shù)字鍵盤
        textFiled.keyboardType = .numberPad
        self.textFiled = textFiled;
        bgView.addSubview(self.textFiled)

textFiled 代理方法的使用

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        if string.characters.count == 0 {//判斷是是否為刪除鍵
            return true
        }else if (textField.text?.characters.count)! >= pointCount {
            //當(dāng)輸入的密碼大于等于6位后就忽略
            return false
        } else {
            return true
        }
    }

每天努力一點(diǎn)點(diǎn)?。。?/p>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容