手势解锁

import UIKit

@objc protocol HZGestureLockViewDelegate {
    func gestureLockView(_ view: HZGestureLockView, didFinished password: String)
}

class HZGestureLockView: UIView {

    private let cols = 3
    private let wh: CGFloat = 50
    private var selectBtns = [UIButton]()
    private var point: CGPoint!
    weak var delegate: HZGestureLockViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        initSubViews()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initSubViews()
    }
    
    // MARK: 初始化9宫格按钮
    private func initSubViews() {
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        let pan = UIPanGestureRecognizer.init(target: self, action: #selector(panAction(_:)))
        self.addGestureRecognizer(pan)
        for i in 0..<9 {
            let btn = UIButton.init(type: .custom)
            btn.isUserInteractionEnabled = false
            // 间距
            let margin = (self.bounds.size.width-CGFloat(cols*50))/CGFloat(cols+1)
            btn.frame = CGRect(x: margin+(margin+wh)*CGFloat((i%cols)), y: margin+(margin+wh)*CGFloat((i/cols)), width: wh, height: wh)
            btn.setImage(UIImage.init(named: "icon-gesture-normal"), for: .normal)
            btn.setImage(UIImage.init(named: "icon-gesture-select"), for: .selected)
            btn.setTitle("\(i)", for: .normal)
            btn.tag = 100+i
            self.addSubview(btn)
            print(i/cols, i%cols)
        }
    }
    
    // MARK: 手势
    @objc func panAction(_ pan: UIPanGestureRecognizer) {
        point = pan.location(in: self)
        setNeedsDisplay()
        for btn in self.subviews {
            if btn.isKind(of: UIButton.self) == false {
                return
            }
            if btn.frame.contains(point) && (btn as! UIButton).isSelected == false {
                (btn as! UIButton).isSelected = true
                selectBtns.append(btn as! UIButton)
            }
        }
        layoutIfNeeded()
        
        var password = ""
        if pan.state == .ended {
            for btn in selectBtns {
                btn.isSelected = false
                let tag = btn.tag-100
                password = password+"\(tag)"
            }
            print(password)
            selectBtns.removeAll()
            delegate?.gestureLockView(self, didFinished: password)
        }
    }
    
    
    override func draw(_ rect: CGRect) {
        if selectBtns.count == 0 {
            return
        }
        kRGBColor(r: 114, g: 138, b: 198).set()
        // 把所有选中按钮中心点连线
        let path = UIBezierPath.init()
        path.lineWidth = 1
        path.lineCapStyle = .round
        path.lineJoinStyle = .round
        for (i, btn) in selectBtns.enumerated() {
            if i == 0 {
                path.move(to: btn.center)
            } else {
                path.addLine(to: btn.center)
            }
        }
        path.addLine(to: point)
        path.stroke()
    }
}

你可能感兴趣的:(手势解锁)