-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSimplePasscodeView.swift
More file actions
146 lines (116 loc) · 4.35 KB
/
Copy pathSimplePasscodeView.swift
File metadata and controls
146 lines (116 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// MHPasscodeView.swift
// SimplePasscodeView
//
// Copyright © 2018 Geeko Coco. All rights reserved.
//
import UIKit
public protocol SimplePasscodeDelegate: class {
func didFinishEntering(_ passcode: String)
func didDeleteBackward()
}
public class SimplePasscodeView: UIView {
@IBOutlet private weak var passcodeStackView: UIStackView!
private var contentView: UIView?
private var passcodeText = String()
public weak var delegate: SimplePasscodeDelegate?
public var keyboardType: UIKeyboardType = .numberPad
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
public override func layoutSubviews() {
super.layoutSubviews()
setup()
}
private func setup() {
if (contentView == nil) {
contentView = loadViewFromNib()
contentView?.frame = bounds
guard let view = contentView else {return}
addSubview(view)
let _ = becomeFirstResponder()
setupPasscodeStackView()
}
}
private func loadViewFromNib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}
}
extension SimplePasscodeView: PasscodeConfigurable {
private func setupPasscodeStackView() {
placeHolderViews.forEach { (view) in
passcodeStackView.addArrangedSubview(view)
}
passcodeStackView.spacing = CGFloat(length)
passcodeStackView.translatesAutoresizingMaskIntoConstraints = false
passcodeStackView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
passcodeStackView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
guard 0..<length ~= customSpacingPosition else { return }
passcodeStackView.distribution = .fill
passcodeStackView.setCustomSpacing(CGFloat(customSpacing),
after: passcodeStackView.arrangedSubviews[customSpacingPosition])
}
private var placeHolderViews: [PinView] {
var views = [PinView]()
for _ in 0..<length {
let pinView: PinView = PinView()
views.append(pinView)
}
return views
}
public func clear() {
UIView.animate(withDuration: 0.3) {
self.passcodeStackView.arrangedSubviews.forEach { (view) in
if let pinView = view as? PinView {
pinView.update(fill: false, andText: nil, isSecureEntry: self.isSecureEntry)
}
}
self.passcodeText.removeAll()
}
}
}
extension SimplePasscodeView: UIKeyInput {
override public var canBecomeFirstResponder: Bool {
return true
}
override public func becomeFirstResponder() -> Bool {
return super.becomeFirstResponder()
}
public var hasText: Bool {
return !passcodeText.isEmpty
}
public func insertText(_ text: String) {
guard canInsertCharacters() else { return }
passcodeText.append(text)
guard let view = passcodeStackView.arrangedSubviews.filter({ (view) -> Bool in
if let pinView = view as? PinView,
pinView.isEmpty() {
return true
}
return false
}).first as? PinView else { return }
view.update(fill: true, andText: text, isSecureEntry: isSecureEntry)
if passcodeText.count == length {
delegate?.didFinishEntering(passcodeText)
}
}
public func deleteBackward() {
guard hasText else { return }
passcodeText.removeLast()
delegate?.didDeleteBackward()
guard let view = passcodeStackView.arrangedSubviews.filter({ (view) -> Bool in
if let pinView = view as? PinView,
!pinView.isEmpty() {
return true
}
return false
}).last as? PinView else { return }
view.update(fill: false, andText: nil, isSecureEntry: isSecureEntry)
}
private func canInsertCharacters() -> Bool {
return passcodeText.count != length
}
}