iOS開發(fā) 如何實(shí)現(xiàn)一個(gè)手勢解鎖

手勢解鎖在一些應(yīng)用中還是會出現(xiàn)的,我們應(yīng)該怎么從零編寫一個(gè)手勢解鎖?下面就一步步介紹怎么實(shí)現(xiàn)(代碼基于swfit語言編寫)

1.為自定義有View添加9個(gè)按鈕,并設(shè)置好布局。

func initChildView() {
        //創(chuàng)建9個(gè)按鈕
        for i in 0...8 {
            let button = UIButton(type: .custom)
            button.isUserInteractionEnabled = false
            let bundle = ZCGestureToUnlockConstant.bunldeForResourec()
            button.setImage(UIImage(named: "gesture_normal", in: bundle, compatibleWith: nil), for: .normal)
            if let selectedImage = selectedImage {
                button.setImage(selectedImage, for: .selected)
            } else {
                button.setImage(UIImage(named: "gesture_selected", in: bundle, compatibleWith: nil), for: .selected)
            }
            button.tag = 1000 + i
            self.addSubview(button)
            self.buttons.append(button)
        }
    }
override func layoutSubviews() {
        super.layoutSubviews()
        if buttons.count <= 0 {
            return
        }
        let count = buttons.count
        let cols = 3
        var x:CGFloat = 0,y:CGFloat = 0,WH:CGFloat = 58
        let margin = (self.bounds.size.width - CGFloat(cols) * WH) / (CGFloat(cols) + 1)
        var col = 0,row = 0
        for i in 0...count - 1{
            col = i % cols
            row = i / cols
            x = margin + (WH + margin) * CGFloat(col)
            y = margin + (WH + margin) *  CGFloat(row)
            let btn = buttons[i]
            btn.frame = CGRect(x: x, y: y, width: WH, height: WH)
        }
    }


2.為手勢解鎖的View添加一個(gè)手勢,并處理手勢事件.

在初始化方法中添加手勢

//添加手勢
        let pan = UIPanGestureRecognizer(target: self, action:#selector(gesturePan(pan:)))
        self.addGestureRecognizer(pan)

處理手勢事件

func gesturePan(pan:UIPanGestureRecognizer) {
        self.currentPoint = pan.location(in: self)
        self.setNeedsDisplay()
        for item in buttons {
            if item.frame.contains(self.currentPoint!) && item.isSelected == false {
                item.isSelected = true
                self.selectedButons.append(item)
            }
        }
//        self.setNeedsLayout()
        if pan.state == .ended {
            var gesturePwd = ""
            for item in self.selectedButons {
                gesturePwd = gesturePwd + String(item.tag - 1000)
                item.isSelected = false
            }
            self.selectedButons.removeAll()
            self.delegate?.passwordDrawRectFinished(unlockView: self, pwd: gesturePwd)
        }
        
    }

獲取到手勢經(jīng)過的按鈕,調(diào)用setNeedsDisplay方法,此時(shí)會調(diào)用View的Draw方法,我們在Draw方法中繪制線條。如果手勢結(jié)束,拿到所有經(jīng)過的按鈕即可拼成一個(gè)字符串,這個(gè)字符串就可以作為手勢密碼。

3.重寫View的Draw方法

override func draw(_ rect: CGRect) {
        super.draw(rect)
        if self.selectedButons.count == 0 {
            return
        }
        let path = UIBezierPath()
        for (index,item) in self.selectedButons.enumerated() {
            if index == 0 {
                path.move(to: item.center)
            } else {
                path.addLine(to: item.center)
            }
        }
        path.addLine(to: self.currentPoint!)
        //顏色可自行設(shè)置,或通過參數(shù)傳進(jìn)來
        if let lineColor = lineColor {
            lineColor.set()
        } else {
            UIColor.colorFromRGB(0xffc8ad).set()
        }
        path.lineJoinStyle = .round
        path.lineWidth = 8
        path.stroke()
    }

以上就是實(shí)現(xiàn)一個(gè)手勢解鎖的基本思路。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容