-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGDTextSlot.swift
200 lines (171 loc) · 4.44 KB
/
GDTextSlot.swift
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// GDTextSlot.swift
//
//
// Created by Saeid Basirnia on 2/21/18.
// Copyright © 2018 Saeid Basirnia. All rights reserved.
//
import UIKit
public protocol GDTextSlotDelegate: class {
func onTextEntered(_ finalText: String)
}
@IBDesignable
final public class GDTextSlot: UIView, UIKeyInput {
public weak var delegate: GDTextSlotDelegate? = nil
private var lock: Bool = false
private var currentSlot: Int = 1
// MARK: - Public variables
public var keyboard: UIKeyboardType = .numberPad
@IBInspectable
public var numberOfSlots: Int = 4 {
didSet {
clearAndGenerate()
}
}
@IBInspectable
public var baseWidth: CGFloat = 50.0 {
didSet {
clearAndGenerate()
}
}
@IBInspectable
public var placeholder: String = "___" {
didSet {
clearAndGenerate()
}
}
public var textFont: UIFont = UIFont.boldSystemFont(ofSize: 25) {
didSet {
clearAndGenerate()
}
}
// MARK: - Properties
public var hasText: Bool {
return currentSlot > 1
}
public override var canBecomeFirstResponder : Bool {
return true
}
public var keyboardType: UIKeyboardType {
get {
return keyboard
}
set {}
}
// MARK: - Init
public override init(frame: CGRect) {
super.init(frame: frame)
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
public override func layoutSubviews() {
if hasText { return }
clearAndGenerate()
}
// MARK: - Insert/Delete text
public func insertText(_ text: String) {
if currentSlot > numberOfSlots { return }
if lock { return }
guard let slot = viewWithTag(currentSlot) as? UILabel else { return }
currentSlot += 1
lock = true
UIView.animate(withDuration: 0.05, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: .curveEaseInOut, animations: {
slot.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
slot.alpha = 0
}) { (_) in
UIView.animate(withDuration: 0.05, delay: 0.0, options: .curveEaseInOut, animations: {
slot.alpha = 1
slot.transform = .identity
slot.text = text
}) { (_) in
self.lock = false
self.updateTextStatus()
}
}
}
public func deleteBackward() {
if currentSlot <= 1 { return }
currentSlot -= 1
guard let slot = viewWithTag(currentSlot) as? UILabel else { return }
UIView.animate(withDuration: 0.2, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: .curveEaseInOut, animations: {
slot.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
slot.alpha = 0
}) { (_) in
UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
slot.alpha = 1
slot.transform = .identity
slot.text = self.placeholder
}, completion: nil)
}
}
// MARK: - Slots generator
public func generateSlots() {
for index in 1...numberOfSlots {
currentSlot = index
addSubview(label)
}
currentSlot = 1
}
private func clearView() {
for v in subviews{
v.removeFromSuperview()
}
}
private func clearAndGenerate() {
clearView()
generateSlots()
}
/// Calculate X position of slot
private var xPosition: CGFloat {
return ((frame.width - (baseWidth * CGFloat(numberOfSlots))) / 2) + (CGFloat((currentSlot - 1)) * CGFloat(baseWidth))
}
private var frameRect: CGRect {
return CGRect(x: xPosition, y: 0, width: baseWidth, height: frame.height)
}
private var currentText: String {
var code: String = ""
for s in 1...numberOfSlots {
guard let slotValue = (viewWithTag(s) as? UILabel)?.text else {
continue
}
code += slotValue
}
return code
}
private var label: UILabel {
let lbl: UILabel = UILabel()
lbl.frame = frameRect
lbl.text = placeholder
lbl.textColor = UIColor.black
lbl.font = textFont
lbl.textAlignment = .center
lbl.tag = currentSlot
return lbl
}
private func updateTextStatus() {
if currentSlot == numberOfSlots + 1 {
delegate?.onTextEntered(currentText)
}
}
private func updateTextStatus(_ text: String) {
let textArray = Array(text)
for s in 1...numberOfSlots {
guard let slotValue = (viewWithTag(s) as? UILabel) else {
continue
}
slotValue.text = String(textArray[s-1])
}
currentSlot = numberOfSlots + 1
delegate?.onTextEntered(text)
}
/// Getting touch to activate the view and call becomeFirstResponder()
override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let firstTouch = touches.first else {
return
}
if firstTouch.view == self {
self.becomeFirstResponder()
}
}
}