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()
}
}
手势解锁
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- GYGestureUnlock Swift版仿支付宝的手势解锁,而且提供方法进行参数修改,能解决项目开发中所有手势...
- 个人原创文章,请尊重原创,转载请注明出处:吴磊的简书:http://www.jianshu.com/p/a4c29...
- 好久没写文章了,最近也比较偷懒,今天继续讨论我实际开发中遇到的需求,那就是关于APP解锁,大家都知道。现在越来越多...