-
Notifications
You must be signed in to change notification settings - Fork 17
/
AdvancedButtonsViewController.swift
344 lines (293 loc) · 14.3 KB
/
AdvancedButtonsViewController.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//
// AdvancedButtonsViewController.swift
// NeoPopExamples
//
// Created by Somesh Karthik on 30/05/22.
// Copyright © 2022 Dreamplug. All rights reserved.
//
import UIKit
import NeoPop
class AdvancedButtonsViewController: UIViewController {
// MARK: View properties
private let titleLabel: UIImageView = {
let view = UIImageView(image: UIImage(named: ImageConstants.advanced))
view.contentMode = .scaleAspectFit
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
private let contentStackView: UIStackView = {
let contentStackView = UIStackView()
contentStackView.translatesAutoresizingMaskIntoConstraints = false
contentStackView.distribution = .equalSpacing
contentStackView.axis = .vertical
contentStackView.alignment = .center
return contentStackView
}()
override func viewDidLoad() {
super.viewDidLoad()
// MARK: Constraints and setup
view.addSubview(titleLabel)
NSLayoutConstraint.activate([
titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
titleLabel.heightAnchor.constraint(equalToConstant: 45),
titleLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: view.frame.height * 0.06)
])
let contentLayoutGuide = UILayoutGuide()
view.addLayoutGuide(contentLayoutGuide)
NSLayoutConstraint.activate([
contentLayoutGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor),
contentLayoutGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor),
contentLayoutGuide.topAnchor.constraint(equalTo: titleLabel.bottomAnchor),
contentLayoutGuide.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
view.addSubview(contentStackView)
NSLayoutConstraint.activate([
contentStackView.widthAnchor.constraint(equalTo: contentLayoutGuide.widthAnchor, multiplier: 0.75),
contentStackView.centerXAnchor.constraint(equalTo: contentLayoutGuide.centerXAnchor),
contentStackView.centerYAnchor.constraint(equalTo: contentLayoutGuide.centerYAnchor),
contentStackView.heightAnchor.constraint(equalTo: contentLayoutGuide.heightAnchor, multiplier: 0.7)
])
contentStackView.addArrangedSubview(getAdjacentButtons())
contentStackView.addArrangedSubview(getConfigButtons())
}
}
// MARK: Adjacent Buttons
private extension AdvancedButtonsViewController {
func getAdjacentButtons() -> UIView {
let stackView = UIStackView()
stackView.axis = .vertical
stackView.spacing = 20
stackView.distribution = .fill
stackView.alignment = .center
let titleView = getTitleView(ImageConstants.adjacentButtons)
titleView.heightAnchor.constraint(equalToConstant: 8).isActive = true
titleView.widthAnchor.constraint(equalToConstant: 80).isActive = true
stackView.addArrangedSubview(titleView)
let borderColor = ColorHelper.popGreen100
let bottomEdge = ColorHelper.popGreen300
let rightEdge = ColorHelper.popGreen200
let topLeftButton = getPopButton(
position: .topLeft,
color: ColorHelper.popWhite500,
tintColor: ColorHelper.contentBackgroundColor,
superViewColor: ColorHelper.contentBackgroundColor,
adjacentButtonModel: AdjacentButtonAvailability(bottom: true, right: true)
)
let topCenterButton = getPopButton(
position: .topEdge,
color: ColorHelper.contentBackgroundColor,
tintColor: ColorHelper.popWhite500,
superViewColor: ColorHelper.popWhite500,
borderColor: borderColor,
adjacentButtonModel: AdjacentButtonAvailability(bottom: true, right: true, left: true)
)
let topRightButton = getPopButton(
position: .topRight,
color: ColorHelper.popWhite500,
tintColor: ColorHelper.contentBackgroundColor,
superViewColor: ColorHelper.contentBackgroundColor, adjacentButtonModel: AdjacentButtonAvailability(bottom: true, left: true)
)
let bottomLeftButton = getPopButton(
position: .bottomLeft,
color: ColorHelper.contentBackgroundColor,
tintColor: ColorHelper.popWhite500,
superViewColor: ColorHelper.popWhite500,
borderColor: borderColor,
edgeColor: EdgeColors(color: bottomEdge),
adjacentButtonModel: AdjacentButtonAvailability(top: true, right: true)
)
let bottomCenterButton = getPopButton(
position: .bottomEdge,
color: ColorHelper.popWhite500,
tintColor: ColorHelper.contentBackgroundColor,
superViewColor: ColorHelper.contentBackgroundColor,
adjacentButtonModel: AdjacentButtonAvailability(top: true, right: true, left: true)
)
let bottomRightButton = getPopButton(
position: .bottomRight,
color: ColorHelper.contentBackgroundColor,
tintColor: ColorHelper.popWhite500,
superViewColor: ColorHelper.popWhite500,
borderColor: borderColor,
edgeColor: EdgeColors(left: nil, right: rightEdge, top: nil, bottom: bottomEdge),
adjacentButtonModel: AdjacentButtonAvailability(top: true, left: true)
)
let buttonsContentStackView = UIStackView()
buttonsContentStackView.axis = .vertical
buttonsContentStackView.spacing = -3
stackView.addArrangedSubview(buttonsContentStackView)
let firstButtonRow = UIStackView()
firstButtonRow.axis = .horizontal
firstButtonRow.spacing = -3
firstButtonRow.translatesAutoresizingMaskIntoConstraints = false
buttonsContentStackView.addArrangedSubview(firstButtonRow)
firstButtonRow.heightAnchor.constraint(equalToConstant: 37).isActive = true
firstButtonRow.addArrangedSubview(topLeftButton)
topLeftButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
firstButtonRow.addArrangedSubview(topCenterButton)
topCenterButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
firstButtonRow.addArrangedSubview(topRightButton)
topRightButton.widthAnchor.constraint(equalToConstant: 63).isActive = true
let secondButtonRow = UIStackView()
secondButtonRow.axis = .horizontal
secondButtonRow.spacing = -3
secondButtonRow.translatesAutoresizingMaskIntoConstraints = false
buttonsContentStackView.addArrangedSubview(secondButtonRow)
secondButtonRow.heightAnchor.constraint(equalToConstant: 40).isActive = true
secondButtonRow.addArrangedSubview(bottomLeftButton)
bottomLeftButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
secondButtonRow.addArrangedSubview(bottomCenterButton)
bottomCenterButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
secondButtonRow.addArrangedSubview(bottomRightButton)
bottomRightButton.widthAnchor.constraint(equalToConstant: 63).isActive = true
return stackView
}
}
// MARK: Config Buttons
private extension AdvancedButtonsViewController {
func getConfigButtons() -> UIView {
let stackView = UIStackView()
stackView.axis = .vertical
stackView.spacing = 20
stackView.distribution = .fill
stackView.alignment = .center
let titleView = getTitleView(ImageConstants.configButtons)
NSLayoutConstraint.activate([
titleView.heightAnchor.constraint(equalToConstant: 8),
titleView.widthAnchor.constraint(equalToConstant: 70)
])
stackView.addArrangedSubview(titleView)
let popView = PopView()
popView.applyNeoPopStyle(
model: PopView.Model(
popEdgeDirection: .bottomRight,
backgroundColor: ColorHelper.contentBackgroundColor
)
)
popView.isUserInteractionEnabled = true
popView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
popView.heightAnchor.constraint(equalToConstant: 138),
popView.widthAnchor.constraint(equalToConstant: 153)
])
stackView.addArrangedSubview(popView)
let bottomEdge = ColorHelper.popGreen300
let rightEdge = ColorHelper.popGreen200
let topLeftButton = getConfigButton(
position: .topLeft
)
let topCenterButton = getConfigButton(
position: .topEdge
)
let topRightButton = getConfigButton(
position: .topRight,
edgeColor: EdgeColors(left: nil, right: rightEdge, top: nil, bottom: nil)
)
let middleLeftButton = getConfigButton(position: .leftEdge)
let middleCenterButton = getConfigButton(position: .center)
let middleRightButton = getConfigButton(
position: .rightEdge,
edgeColor: EdgeColors(left: nil, right: rightEdge, top: nil, bottom: nil)
)
let bottomLeftButton = getConfigButton(
position: .bottomLeft,
edgeColor: EdgeColors(left: nil, right: nil, top: nil, bottom: bottomEdge)
)
let bottomCenterButton = getConfigButton(
position: .bottomEdge,
edgeColor: EdgeColors(left: nil, right: nil, top: nil, bottom: bottomEdge)
)
let bottomRightButton = getConfigButton(
position: .bottomRight,
edgeColor: EdgeColors(left: nil, right: rightEdge, top: nil, bottom: bottomEdge)
)
let buttonsContentStackView = UIStackView()
buttonsContentStackView.axis = .vertical
buttonsContentStackView.distribution = .equalSpacing
popView.addSubview(buttonsContentStackView)
buttonsContentStackView.fillSuperview()
let topButtonRow = UIStackView()
topButtonRow.axis = .horizontal
topButtonRow.distribution = .equalSpacing
topButtonRow.translatesAutoresizingMaskIntoConstraints = false
buttonsContentStackView.addArrangedSubview(topButtonRow)
topButtonRow.heightAnchor.constraint(equalToConstant: 40).isActive = true
topButtonRow.addArrangedSubview(topLeftButton)
topLeftButton.widthAnchor.constraint(equalToConstant: 43).isActive = true
topButtonRow.addArrangedSubview(topCenterButton)
topCenterButton.widthAnchor.constraint(equalToConstant: 43).isActive = true
topButtonRow.addArrangedSubview(topRightButton)
topRightButton.widthAnchor.constraint(equalToConstant: 43).isActive = true
let middleButtonRow = UIStackView()
middleButtonRow.axis = .horizontal
middleButtonRow.distribution = .equalSpacing
middleButtonRow.translatesAutoresizingMaskIntoConstraints = false
buttonsContentStackView.addArrangedSubview(middleButtonRow)
middleButtonRow.heightAnchor.constraint(equalToConstant: 40).isActive = true
middleButtonRow.addArrangedSubview(middleLeftButton)
middleLeftButton.widthAnchor.constraint(equalToConstant: 43).isActive = true
middleButtonRow.addArrangedSubview(middleCenterButton)
middleCenterButton.widthAnchor.constraint(equalToConstant: 43).isActive = true
middleButtonRow.addArrangedSubview(middleRightButton)
middleRightButton.widthAnchor.constraint(equalToConstant: 43).isActive = true
let bottomButtonRow = UIStackView()
bottomButtonRow.axis = .horizontal
bottomButtonRow.distribution = .equalSpacing
bottomButtonRow.translatesAutoresizingMaskIntoConstraints = false
buttonsContentStackView.addArrangedSubview(bottomButtonRow)
bottomButtonRow.heightAnchor.constraint(equalToConstant: 40).isActive = true
bottomButtonRow.addArrangedSubview(bottomLeftButton)
bottomLeftButton.widthAnchor.constraint(equalToConstant: 43).isActive = true
bottomButtonRow.addArrangedSubview(bottomCenterButton)
bottomCenterButton.widthAnchor.constraint(equalToConstant: 43).isActive = true
bottomButtonRow.addArrangedSubview(bottomRightButton)
bottomRightButton.widthAnchor.constraint(equalToConstant: 43).isActive = true
return stackView
}
func getConfigButton(position: PopButton.Position,
edgeColor: EdgeColors? = nil,
adjacentButtonModel: AdjacentButtonAvailability? = nil) -> UIView {
getPopButton(position: position, color: ColorHelper.contentBackgroundColor, tintColor: ColorHelper.popWhite500, superViewColor: ColorHelper.contentBackgroundColor, borderColor: ColorHelper.popGreen100, edgeColor: edgeColor, imageScale: 2, adjacentButtonModel: adjacentButtonModel)
}
}
// MARK: Common Methods
private extension AdvancedButtonsViewController {
func getPopButton(position: PopButton.Position,
color: UIColor,
tintColor: UIColor,
superViewColor: UIColor,
borderColor: UIColor? = nil,
edgeColor: EdgeColors? = nil,
imageScale: CGFloat = 2.33,
adjacentButtonModel: AdjacentButtonAvailability? = nil) -> UIView {
let popButton = PopButton()
popButton.translatesAutoresizingMaskIntoConstraints = false
popButton.configurePopButton(
withModel: PopButton.Model(
position: position,
backgroundColor: color,
superViewColor: superViewColor,
buttonFaceBorderColor: EdgeColors(color: borderColor),
borderWidth: 0.31,
customEdgeColor: edgeColor
)
)
popButton.configureButtonContent(
withModel: PopButtonContainerView.Model(
title: nil,
leftImage: UIImage(named: ImageConstants.button),
leftImageTintColor: tintColor,
leftImageScale: imageScale,
contentLeftRightInset: 0
)
)
return popButton
}
func getTitleView(_ text: String) -> UIView {
let imageView = UIImageView()
imageView.image = UIImage(named: text)!
imageView.contentMode = .scaleAspectFit
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}
}