iOS开发 如何实现一个手势解锁

手势解锁在一些应用中还是会出现的,我们应该怎么从零编写一个手势解锁?下面就一步步介绍怎么实现(代码基于swfit语言编写)

1.为自定义有View添加9个按钮,并设置好布局。

func initChildView() {
        //创建9个按钮
        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添加一个手势,并处理手势事件.

在初始化方法中添加手势

//添加手势
        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)
        }
        
    }

获取到手势经过的按钮,调用setNeedsDisplay方法,此时会调用View的Draw方法,我们在Draw方法中绘制线条。如果手势结束,拿到所有经过的按钮即可拼成一个字符串,这个字符串就可以作为手势密码。

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!)
        //颜色可自行设置,或通过参数传进来
        if let lineColor = lineColor {
            lineColor.set()
        } else {
            UIColor.colorFromRGB(0xffc8ad).set()
        }
        path.lineJoinStyle = .round
        path.lineWidth = 8
        path.stroke()
    }

以上就是实现一个手势解锁的基本思路。

你可能感兴趣的:(iOS开发 如何实现一个手势解锁)