-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvancedSegmentedControl.swift
More file actions
232 lines (164 loc) · 7.49 KB
/
Copy pathAdvancedSegmentedControl.swift
File metadata and controls
232 lines (164 loc) · 7.49 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// AdvancedSegmentedControl.swift
//
// Created by Tope Abayomi on 01/12/2014
// modified by Mario Alejandro Ramos for swift 4.2
// Copyright © 2018 Apolo Mobile E.I.R.L. All rights reserved.
//
import UIKit
@IBDesignable class AdvancedSegmentedControl: UIControl {
private var labels = [UILabel]()
var thumbView = UIView()
// items setup
var items: [String] = ["Unidad 1", "Unidad 2", "Unidad 3", "Unidad 4"," Todas "] {
didSet {
setupLabels()
}
}
//Here we select the initial index
var selectedIndex : Int = 4 {
didSet {
displayNewSelectedIndex()
}
}
@IBInspectable var selectedLabelColor : UIColor = UIColor.black {
didSet {
setSelectedColors()
}
}
@IBInspectable var unselectedLabelColor : UIColor = UIColor.white {
didSet {
setSelectedColors()
}
}
@IBInspectable var thumbColor : UIColor = UIColor.white {
didSet {
setSelectedColors()
}
}
@IBInspectable var borderColor : UIColor = UIColor.white {
didSet {
layer.borderColor = borderColor.cgColor
}
}
@IBInspectable var font : UIFont! = UIFont.systemFont(ofSize: 12) {
didSet {
setFont()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init(coder: NSCoder) {
super.init(coder: coder)!
setupView()
}
func setupView(){
layer.cornerRadius = frame.height / 2
layer.borderColor = UIColor(white: 1.0, alpha: 0.5).cgColor
layer.borderWidth = 4
backgroundColor = UIColor.white
setupLabels()
addIndividualItemConstraints(items: labels, mainView: self, padding: 0)
insertSubview(thumbView, at: 0)
}
func setupLabels(){
for label in labels {
label.removeFromSuperview()
}
labels.removeAll(keepingCapacity: true)
for index in 1...items.count {
let label = UILabel(frame: CGRect(0, 0, 70, 40))
label.text = items[index - 1]
label.backgroundColor = UIColor.clear
label.textAlignment = .center
label.font = UIFont(name: "Avenir-Black", size: 15)
label.textColor = index == 1 ? selectedLabelColor : unselectedLabelColor
label.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(label)
labels.append(label)
}
addIndividualItemConstraints(items: labels, mainView: self, padding: 0)
}
override func layoutSubviews() {
super.layoutSubviews()
var selectFrame = self.bounds
let newWidth = selectFrame.width / CGFloat(items.count)
selectFrame.size.width = newWidth
thumbView.frame = selectFrame
thumbView.backgroundColor = thumbColor
thumbView.layer.cornerRadius = thumbView.frame.height / 2
displayNewSelectedIndex()
}
override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
let location = touch.location(in: self)
var calculatedIndex : Int?
for (index, item) in labels.enumerated() {
if item.frame.contains(location) {
calculatedIndex = index
}
}
if calculatedIndex != nil {
selectedIndex = calculatedIndex!
sendActions(for: .valueChanged)
print("***** I've selected the index :\(calculatedIndex!)")
}
return false
}
func displayNewSelectedIndex(){
for (_, item) in labels.enumerated() {
item.textColor = unselectedLabelColor
}
let label = labels[selectedIndex]
label.textColor = selectedLabelColor
UIView.animate(withDuration:0.4, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.4, animations: {
self.thumbView.frame = label.frame
}, completion: nil)
}
func addIndividualItemConstraints(items: [UIView], mainView: UIView, padding: CGFloat) {
_ = mainView.constraints
for (index, button) in items.enumerated() {
let topConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: mainView, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1.0, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: mainView, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1.0, constant: 0)
var rightConstraint : NSLayoutConstraint!
if index == items.count - 1 {
rightConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.right, relatedBy: NSLayoutConstraint.Relation.equal, toItem: mainView, attribute: NSLayoutConstraint.Attribute.right, multiplier: 1.0, constant: -padding)
}else{
let nextButton = items[index+1]
rightConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.right, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nextButton, attribute: NSLayoutConstraint.Attribute.left, multiplier: 1.0, constant: -padding)
}
var leftConstraint : NSLayoutConstraint!
if index == 0 {
leftConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: mainView, attribute: NSLayoutConstraint.Attribute.left, multiplier: 1.0, constant: padding)
}else{
let prevButton = items[index-1]
leftConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: prevButton, attribute: NSLayoutConstraint.Attribute.right, multiplier: 1.0, constant: padding)
let firstItem = items[0]
let widthConstraint = NSLayoutConstraint(item: button, attribute: .width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: firstItem, attribute: .width, multiplier: 1.0 , constant: 0)
mainView.addConstraint(widthConstraint)
}
mainView.addConstraints([topConstraint, bottomConstraint, rightConstraint, leftConstraint])
}
}
func setSelectedColors(){
for item in labels {
item.textColor = unselectedLabelColor
}
if labels.count > 0 {
labels[0].textColor = selectedLabelColor
}
thumbView.backgroundColor = thumbColor
}
func setFont(){
for item in labels {
item.font = font
}
}
}
//For CGRect supports Swift 4.2
extension CGRect {
init(_ x:CGFloat, _ y:CGFloat, _ w:CGFloat, _ h:CGFloat) {
self.init(x:x, y:y, width:w, height:h)
}
}