CameraViewControllerConstraint.swift
17.9 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//
// CameraViewControllerConstraint.swift
// CameraViewControllerConstraint
//
// Created by Pedro Paulo de Amorim.
// Copyright (c) 2016 zero. All rights reserved.
//
import UIKit
import AVFoundation
/**
* This extension provides the configuration of
* constraints for CameraViewController.
*/
extension CameraViewController {
/**
* To attach the view to the edges of the superview, it needs
to be pinned on the sides of the self.view, based on the
edges of this superview.
* This configure the cameraView to show, in real time, the
* camera.
*/
func configCameraViewConstraints() {
[.left, .right, .top, .bottom].forEach({
view.addConstraint(NSLayoutConstraint(
item: cameraView,
attribute: $0,
relatedBy: .equal,
toItem: view,
attribute: $0,
multiplier: 1.0,
constant: 0))
})
}
/**
* Add the constraints based on the device orientation,
* this pin the button on the bottom part of the screen
* when the device is portrait, when landscape, pin
* the button on the right part of the screen.
*/
func configCameraButtonEdgeConstraint(_ statusBarOrientation: UIInterfaceOrientation) {
view.autoRemoveConstraint(cameraButtonEdgeConstraint)
let attribute : NSLayoutAttribute = {
switch statusBarOrientation {
case .portrait: return .bottom
case .landscapeRight: return .right
case .landscapeLeft: return .left
default: return .top
}
}()
cameraButtonEdgeConstraint = NSLayoutConstraint(
item: cameraButton,
attribute: attribute,
relatedBy: .equal,
toItem: view,
attribute: attribute,
multiplier: 1.0,
constant: attribute == .right || attribute == .bottom ? -8 : 8)
view.addConstraint(cameraButtonEdgeConstraint!)
}
/**
* Add the constraints based on the device orientation,
* centerX the button based on the width of screen.
* When the device is landscape orientation, centerY
* the button based on the height of screen.
*/
func configCameraButtonGravityConstraint(_ portrait: Bool) {
view.autoRemoveConstraint(cameraButtonGravityConstraint)
let attribute : NSLayoutAttribute = portrait ? .centerX : .centerY
cameraButtonGravityConstraint = NSLayoutConstraint(
item: cameraButton,
attribute: attribute,
relatedBy: .equal,
toItem: view,
attribute: attribute,
multiplier: 1.0,
constant: 0)
view.addConstraint(cameraButtonGravityConstraint!)
}
/**
* Remove the constraints of container.
*/
func removeContainerConstraints() {
view.autoRemoveConstraint(containerButtonsEdgeOneConstraint)
view.autoRemoveConstraint(containerButtonsEdgeTwoConstraint)
view.autoRemoveConstraint(containerButtonsGravityConstraint)
}
/**
* Configure the edges constraints of container that
* handle the center position of SwapButton and
* LibraryButton.
*/
func configContainerEdgeConstraint(_ statusBarOrientation : UIInterfaceOrientation) {
let attributeOne : NSLayoutAttribute
let attributeTwo : NSLayoutAttribute
switch statusBarOrientation {
case .portrait:
attributeOne = .left
attributeTwo = .right
break
case .landscapeRight:
attributeOne = .bottom
attributeTwo = .top
break
case .landscapeLeft:
attributeOne = .top
attributeTwo = .bottom
break
default:
attributeOne = .right
attributeTwo = .left
break
}
containerButtonsEdgeOneConstraint = NSLayoutConstraint(
item: containerSwapLibraryButton,
attribute: attributeOne,
relatedBy: .equal,
toItem: cameraButton,
attribute: attributeTwo,
multiplier: 1.0,
constant: 0)
view.addConstraint(containerButtonsEdgeOneConstraint!)
containerButtonsEdgeTwoConstraint = NSLayoutConstraint(
item: containerSwapLibraryButton,
attribute: attributeTwo,
relatedBy: .equal,
toItem: view,
attribute: attributeTwo,
multiplier: 1.0,
constant: 0)
view.addConstraint(containerButtonsEdgeTwoConstraint!)
}
/**
* Configure the gravity of container, based on the
* orientation of the device.
*/
func configContainerGravityConstraint(_ statusBarOrientation : UIInterfaceOrientation) {
let attributeCenter : NSLayoutAttribute = statusBarOrientation.isPortrait ? .centerY : .centerX
containerButtonsGravityConstraint = NSLayoutConstraint(
item: containerSwapLibraryButton,
attribute: attributeCenter,
relatedBy: .equal,
toItem: cameraButton,
attribute: attributeCenter,
multiplier: 1.0,
constant: 0)
view.addConstraint(containerButtonsGravityConstraint!)
}
/**
* Remove the SwapButton constraints to be updated when
* the device was rotated.
*/
func removeSwapButtonConstraints() {
view.autoRemoveConstraint(swapButtonEdgeOneConstraint)
view.autoRemoveConstraint(swapButtonEdgeTwoConstraint)
view.autoRemoveConstraint(swapButtonGravityConstraint)
}
/**
* If the device is portrait, pin the SwapButton on the
* right side of the CameraButton.
* If landscape, pin the SwapButton on the top of the
* CameraButton.
*/
func configSwapButtonEdgeConstraint(_ statusBarOrientation : UIInterfaceOrientation) {
let attributeOne : NSLayoutAttribute
let attributeTwo : NSLayoutAttribute
switch statusBarOrientation {
case .portrait:
attributeOne = .top
attributeTwo = .bottom
break
case .landscapeRight:
attributeOne = .left
attributeTwo = .right
break
case .landscapeLeft:
attributeOne = .right
attributeTwo = .left
break
default:
attributeOne = .bottom
attributeTwo = .top
break
}
swapButtonEdgeOneConstraint = NSLayoutConstraint(
item: swapButton,
attribute: attributeOne,
relatedBy: .equal,
toItem: containerSwapLibraryButton,
attribute: attributeOne,
multiplier: 1.0,
constant: 0)
view.addConstraint(swapButtonEdgeOneConstraint!)
swapButtonEdgeTwoConstraint = NSLayoutConstraint(
item: swapButton,
attribute: attributeTwo,
relatedBy: .equal,
toItem: containerSwapLibraryButton,
attribute: attributeTwo,
multiplier: 1.0,
constant: 0)
view.addConstraint(swapButtonEdgeTwoConstraint!)
}
/**
* Configure the center of SwapButton, based on the
* axis center of CameraButton.
*/
func configSwapButtonGravityConstraint(_ portrait: Bool) {
swapButtonGravityConstraint = NSLayoutConstraint(
item: swapButton,
attribute: portrait ? .right : .bottom,
relatedBy: .lessThanOrEqual,
toItem: containerSwapLibraryButton,
attribute: portrait ? .centerX : .centerY,
multiplier: 1.0,
constant: -4.0 * DeviceConfig.SCREEN_MULTIPLIER)
view.addConstraint(swapButtonGravityConstraint!)
}
func removeCloseButtonConstraints() {
view.autoRemoveConstraint(closeButtonEdgeConstraint)
view.autoRemoveConstraint(closeButtonGravityConstraint)
}
/**
* Pin the close button to the left of the superview.
*/
func configCloseButtonEdgeConstraint(_ statusBarOrientation : UIInterfaceOrientation) {
let attribute : NSLayoutAttribute = {
switch statusBarOrientation {
case .portrait: return .left
case .landscapeRight, .landscapeLeft: return .centerX
default: return .right
}
}()
closeButtonEdgeConstraint = NSLayoutConstraint(
item: closeButton,
attribute: attribute,
relatedBy: .equal,
toItem: attribute != .centerX ? view : cameraButton,
attribute: attribute,
multiplier: 1.0,
constant: attribute != .centerX ? 16 : 0)
view.addConstraint(closeButtonEdgeConstraint!)
}
/**
* Add the constraint for the CloseButton, based on
* the device orientation.
* If portrait, it pin the CloseButton on the CenterY
* of the CameraButton.
* Else if landscape, pin this button on the Bottom
* of superview.
*/
func configCloseButtonGravityConstraint(_ statusBarOrientation : UIInterfaceOrientation) {
let attribute : NSLayoutAttribute
let constant : CGFloat
switch statusBarOrientation {
case .portrait:
attribute = .centerY
constant = 0.0
break
case .landscapeRight:
attribute = .bottom
constant = -16.0
break
case .landscapeLeft:
attribute = .top
constant = 16.0
break
default:
attribute = .centerX
constant = 0.0
break
}
closeButtonGravityConstraint = NSLayoutConstraint(
item: closeButton,
attribute: attribute,
relatedBy: .equal,
toItem: attribute == .bottom || attribute == .top ? view : cameraButton,
attribute: attribute,
multiplier: 1.0,
constant: constant)
view.addConstraint(closeButtonGravityConstraint!)
}
/**
* Remove the LibraryButton constraints to be updated when
* the device was rotated.
*/
func removeLibraryButtonConstraints() {
view.autoRemoveConstraint(libraryButtonEdgeOneConstraint)
view.autoRemoveConstraint(libraryButtonEdgeTwoConstraint)
view.autoRemoveConstraint(libraryButtonGravityConstraint)
}
/**
* Add the constraint of the LibraryButton, if the device
* orientation is portrait, pin the right side of SwapButton
* to the left side of LibraryButton.
* If landscape, pin the bottom side of CameraButton on the
* top side of LibraryButton.
*/
func configLibraryEdgeButtonConstraint(_ statusBarOrientation : UIInterfaceOrientation) {
let attributeOne : NSLayoutAttribute
let attributeTwo : NSLayoutAttribute
switch statusBarOrientation {
case .portrait:
attributeOne = .top
attributeTwo = .bottom
break
case .landscapeRight:
attributeOne = .left
attributeTwo = .right
break
case .landscapeLeft:
attributeOne = .right
attributeTwo = .left
break
default:
attributeOne = .bottom
attributeTwo = .top
break
}
libraryButtonEdgeOneConstraint = NSLayoutConstraint(
item: libraryButton,
attribute: attributeOne,
relatedBy: .equal,
toItem: containerSwapLibraryButton,
attribute: attributeOne,
multiplier: 1.0,
constant: 0)
view.addConstraint(libraryButtonEdgeOneConstraint!)
libraryButtonEdgeTwoConstraint = NSLayoutConstraint(
item: libraryButton,
attribute: attributeTwo,
relatedBy: .equal,
toItem: containerSwapLibraryButton,
attribute: attributeTwo,
multiplier: 1.0,
constant: 0)
view.addConstraint(libraryButtonEdgeTwoConstraint!)
}
/**
* Set the center gravity of the LibraryButton based
* on the position of CameraButton.
*/
func configLibraryGravityButtonConstraint(_ portrait: Bool) {
libraryButtonGravityConstraint = NSLayoutConstraint(
item: libraryButton,
attribute: portrait ? .left : .top,
relatedBy: .lessThanOrEqual,
toItem: containerSwapLibraryButton,
attribute: portrait ? .centerX : .centerY,
multiplier: 1.0,
constant: 4.0 * DeviceConfig.SCREEN_MULTIPLIER)
view.addConstraint(libraryButtonGravityConstraint!)
}
/**
* If the device orientation is portrait, pin the top of
* FlashButton to the top side of superview.
* Else if, pin the FlashButton bottom side on the top side
* of SwapButton.
*/
func configFlashEdgeButtonConstraint(_ statusBarOrientation: UIInterfaceOrientation) {
view.autoRemoveConstraint(flashButtonEdgeConstraint)
let constraintRight = statusBarOrientation == .portrait || statusBarOrientation == .landscapeRight
let attribute : NSLayoutAttribute = constraintRight ? .top : .bottom
flashButtonEdgeConstraint = NSLayoutConstraint(
item: flashButton,
attribute: attribute,
relatedBy: .equal,
toItem: view,
attribute: attribute,
multiplier: 1.0,
constant: constraintRight ? 8 : -8)
view.addConstraint(flashButtonEdgeConstraint!)
}
/**
* If the device orientation is portrait, pin the
right side of FlashButton to the right side of
* superview.
* Else if, centerX the FlashButton on the CenterX
* of CameraButton.
*/
func configFlashGravityButtonConstraint(_ statusBarOrientation: UIInterfaceOrientation) {
view.autoRemoveConstraint(flashButtonGravityConstraint)
let constraintRight = statusBarOrientation == .portrait || statusBarOrientation == .landscapeLeft
let attribute : NSLayoutAttribute = constraintRight ? .right : .left
flashButtonGravityConstraint = NSLayoutConstraint(
item: flashButton,
attribute: attribute,
relatedBy: .equal,
toItem: view,
attribute: attribute,
multiplier: 1.0,
constant: constraintRight ? -8 : 8)
view.addConstraint(flashButtonGravityConstraint!)
}
/**
* Used to create a perfect square for CameraOverlay.
* This method will determinate the size of CameraOverlay,
* if portrait, it will use the width of superview to
* determinate the height of the view. Else if landscape,
* it uses the height of the superview to create the width
* of the CameraOverlay.
*/
func configCameraOverlayWidthConstraint(_ portrait: Bool) {
view.autoRemoveConstraint(cameraOverlayWidthConstraint)
cameraOverlayWidthConstraint = NSLayoutConstraint(
item: cameraOverlay,
attribute: portrait ? .height : .width,
relatedBy: .equal,
toItem: cameraOverlay,
attribute: portrait ? .width : .height,
multiplier: 1.0,
constant: 0)
view.addConstraint(cameraOverlayWidthConstraint!)
}
/**
* This method will center the relative position of
* CameraOverlay, based on the biggest size of the
* superview.
*/
func configCameraOverlayCenterConstraint(_ portrait: Bool) {
view.autoRemoveConstraint(cameraOverlayCenterConstraint)
let attribute : NSLayoutAttribute = portrait ? .centerY : .centerX
cameraOverlayCenterConstraint = NSLayoutConstraint(
item: cameraOverlay,
attribute: attribute,
relatedBy: .equal,
toItem: view,
attribute: attribute,
multiplier: 1.0,
constant: 0)
view.addConstraint(cameraOverlayCenterConstraint!)
}
/**
* Remove the CameraOverlay constraints to be updated when
* the device was rotated.
*/
func removeCameraOverlayEdgesConstraints() {
view.autoRemoveConstraint(cameraOverlayEdgeOneConstraint)
view.autoRemoveConstraint(cameraOverlayEdgeTwoConstraint)
}
/**
* It needs to get a determined smallest size of the screen
to create the smallest size to be used on CameraOverlay.
It uses the orientation of the screen to determinate where
the view will be pinned.
*/
func configCameraOverlayEdgeOneContraint(_ portrait: Bool, padding: CGFloat) {
let attribute : NSLayoutAttribute = portrait ? .left : .bottom
cameraOverlayEdgeOneConstraint = NSLayoutConstraint(
item: cameraOverlay,
attribute: attribute,
relatedBy: .equal,
toItem: view,
attribute: attribute,
multiplier: 1.0,
constant: padding)
view.addConstraint(cameraOverlayEdgeOneConstraint!)
}
/**
* It needs to get a determined smallest size of the screen
to create the smallest size to be used on CameraOverlay.
It uses the orientation of the screen to determinate where
the view will be pinned.
*/
func configCameraOverlayEdgeTwoConstraint(_ portrait: Bool, padding: CGFloat) {
let attributeTwo : NSLayoutAttribute = portrait ? .right : .top
cameraOverlayEdgeTwoConstraint = NSLayoutConstraint(
item: cameraOverlay,
attribute: attributeTwo,
relatedBy: .equal,
toItem: view,
attribute: attributeTwo,
multiplier: 1.0,
constant: -padding)
view.addConstraint(cameraOverlayEdgeTwoConstraint!)
}
}