-
Notifications
You must be signed in to change notification settings - Fork 62
/
DodoDistrib.swift
3146 lines (2217 loc) · 80.1 KB
/
DodoDistrib.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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Dodo
//
// A message bar for iOS.
//
// https://github.com/evgenyneu/Dodo
//
// This file was automatically generated by combining multiple Swift source files.
//
// ----------------------------
//
// DodoButtonView.swift
//
// ----------------------------
import UIKit
class DodoButtonView: UIImageView {
private let style: DodoButtonStyle
weak var delegate: DodoButtonViewDelegate?
var onTap: OnTap?
init(style: DodoButtonStyle) {
self.style = style
super.init(frame: CGRect())
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// Create button views for given button styles.
static func createMany(_ styles: [DodoButtonStyle]) -> [DodoButtonView] {
if !haveButtons(styles) { return [] }
return styles.map { style in
let view = DodoButtonView(style: style)
view.setup()
return view
}
}
static func haveButtons(_ styles: [DodoButtonStyle]) -> Bool {
let hasImages = styles.filter({ $0.image != nil }).count > 0
let hasIcons = styles.filter({ $0.icon != nil }).count > 0
return hasImages || hasIcons
}
func doLayout(onLeftSide: Bool) {
precondition(delegate != nil, "Button view delegate can not be nil")
translatesAutoresizingMaskIntoConstraints = false
// Set button's size
TegAutolayoutConstraints.width(self, value: style.size.width)
TegAutolayoutConstraints.height(self, value: style.size.height)
if let superview = superview {
let alignAttribute = onLeftSide ? NSLayoutConstraint.Attribute.left : NSLayoutConstraint.Attribute.right
let marginHorizontal = onLeftSide ? style.horizontalMarginToBar : -style.horizontalMarginToBar
// Align the button to the left/right of the view
TegAutolayoutConstraints.alignSameAttributes(self, toItem: superview,
constraintContainer: superview,
attribute: alignAttribute, margin: marginHorizontal)
// Center the button verticaly
TegAutolayoutConstraints.centerY(self, viewTwo: superview, constraintContainer: superview)
}
}
func setup() {
if let image = DodoButtonView.image(style) { applyStyle(image) }
setupTap()
}
/// Increase the hitsize of the image view if it's less than 44px for easier tapping.
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
let oprimizedBounds = DodoTouchTarget.optimize(bounds)
return oprimizedBounds.contains(point)
}
/// Returns the image supplied by user or create one from the icon
class func image(_ style: DodoButtonStyle) -> UIImage? {
if style.image != nil {
return style.image
}
if let icon = style.icon {
let bundle = Bundle(for: self)
let imageName = icon.rawValue
return UIImage(named: imageName, in: bundle, compatibleWith: nil)
}
return nil
}
private func applyStyle(_ imageIn: UIImage) {
var imageToShow = imageIn
if let tintColorToShow = style.tintColor {
// Replace image colors with the specified tint color
imageToShow = imageToShow.withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
tintColor = tintColorToShow
}
layer.minificationFilter = CALayerContentsFilter.trilinear // make the image crisp
image = imageToShow
contentMode = UIView.ContentMode.scaleAspectFit
// Make button accessible
if let accessibilityLabelToShow = style.accessibilityLabel {
isAccessibilityElement = true
accessibilityLabel = accessibilityLabelToShow
accessibilityTraits = UIAccessibilityTraits.button
}
}
private func setupTap() {
onTap = OnTap(view: self, gesture: UITapGestureRecognizer()) { [weak self] in
self?.didTap()
}
}
private func didTap() {
self.delegate?.buttonDelegateDidTap(self.style)
style.onTap?()
}
}
// ----------------------------
//
// DodoButtonViewDelegate.swift
//
// ----------------------------
protocol DodoButtonViewDelegate: class {
func buttonDelegateDidTap(_ buttonStyle: DodoButtonStyle)
}
// ----------------------------
//
// DodoButtonOnTap.swift
//
// ----------------------------
/// A closure that is called when a bar button is tapped
public typealias DodoButtonOnTap = ()->()
// ----------------------------
//
// DodoInterface.swift
//
// ----------------------------
import UIKit
/**
Coordinates the process of showing and hiding of the message bar.
The instance is created automatically in the `dodo` property of any UIView instance.
It is not expected to be instantiated manually anywhere except unit tests.
For example:
let view = UIView()
view.dodo.info("Horses are blue?")
*/
public protocol DodoInterface: class {
/**
Specify optional anchor for positioning the bar view.
This can be an anchor from the safe area.
*/
var topAnchor: NSLayoutYAxisAnchor? { get set }
/**
Specify optional anchor for positioning the bar view.
This can be an anchor from the safe area.
*/
var bottomAnchor: NSLayoutYAxisAnchor? { get set }
/// Specify optional layout guide for positioning the bar view.
@available(*, deprecated, message: "use topAnchor instead")
var topLayoutGuide: UILayoutSupport? { get set }
/// Specify optional layout guide for positioning the bar view.
@available(*, deprecated, message: "use bottomAnchor instead")
var bottomLayoutGuide: UILayoutSupport? { get set }
/// Defines styles for the bar.
var style: DodoStyle { get set }
/// Changes the style preset for the bar widget.
var preset: DodoPresets { get set }
/**
Shows the message bar with *.success* preset. It can be used to indicate successful completion of an operation.
- parameter message: The text message to be shown.
*/
func success(_ message: String)
/**
Shows the message bar with *.Info* preset. It can be used for showing information messages that have neutral emotional value.
- parameter message: The text message to be shown.
*/
func info(_ message: String)
/**
Shows the message bar with *.warning* preset. It can be used for for showing warning messages.
- parameter message: The text message to be shown.
*/
func warning(_ message: String)
/**
Shows the message bar with *.warning* preset. It can be used for showing critical error messages
- parameter message: The text message to be shown.
*/
func error(_ message: String)
/**
Shows the message bar. Set `preset` property to change the appearance of the message bar, or use the shortcut methods: `success`, `info`, `warning` and `error`.
- parameter message: The text message to be shown.
*/
func show(_ message: String)
/// Hide the message bar if it's currently shown.
func hide()
}
// ----------------------------
//
// Dodo.swift
//
// ----------------------------
import UIKit
/**
Main class that coordinates the process of showing and hiding of the message bar.
Instance of this class is created automatically in the `dodo` property of any UIView instance.
It is not expected to be instantiated manually anywhere except unit tests.
For example:
let view = UIView()
view.dodo.info("Horses are blue?")
*/
final class Dodo: DodoInterface, DodoButtonViewDelegate {
private weak var superview: UIView!
private var hideTimer: MoaTimer?
// Gesture handler that hides the bar when it is tapped
var onTap: OnTap?
/**
Specify optional anchor for positioning the bar view.
This can be an anchor from the safe area.
*/
var topAnchor: NSLayoutYAxisAnchor?
/**
Specify optional anchor for positioning the bar view.
This can be an anchor from the safe area.
*/
var bottomAnchor: NSLayoutYAxisAnchor?
/// Specify optional layout guide for positioning the bar view. Deprecated, use bottomAnchor instead.
@available(*, deprecated, message: "use topAnchor instead")
var topLayoutGuide: UILayoutSupport? {
set { self.topAnchor = newValue?.bottomAnchor }
get { return nil }
}
/// Specify optional layout guide for positioning the bar view. Deprecated, use bottomAnchor instead.
@available(*, deprecated, message: "use bottomAnchor instead")
var bottomLayoutGuide: UILayoutSupport? {
set { self.bottomAnchor = newValue?.topAnchor }
get { return nil }
}
/// Defines styles for the bar.
var style = DodoStyle(parentStyle: DodoPresets.defaultPreset.style)
/// Creates an instance of Dodo class
init(superview: UIView) {
self.superview = superview
DodoKeyboardListener.startListening()
}
/// Changes the style preset for the bar widget.
var preset: DodoPresets = DodoPresets.defaultPreset {
didSet {
if preset != oldValue {
style.parent = preset.style
}
}
}
/**
Shows the message bar with *.success* preset. It can be used to indicate successful completion of an operation.
- parameter message: The text message to be shown.
*/
func success(_ message: String) {
preset = .success
show(message)
}
/**
Shows the message bar with *.Info* preset. It can be used for showing information messages that have neutral emotional value.
- parameter message: The text message to be shown.
*/
func info(_ message: String) {
preset = .info
show(message)
}
/**
Shows the message bar with *.warning* preset. It can be used for for showing warning messages.
- parameter message: The text message to be shown.
*/
func warning(_ message: String) {
preset = .warning
show(message)
}
/**
Shows the message bar with *.warning* preset. It can be used for showing critical error messages
- parameter message: The text message to be shown.
*/
func error(_ message: String) {
preset = .error
show(message)
}
/**
Shows the message bar. Set `preset` property to change the appearance of the message bar, or use the shortcut methods: `success`, `info`, `warning` and `error`.
- parameter message: The text message to be shown.
*/
func show(_ message: String) {
removeExistingBars()
setupHideTimer()
let bar = DodoToolbar(witStyle: style)
setupHideOnTap(bar)
bar.anchor = style.bar.locationTop ? topAnchor: bottomAnchor
bar.buttonViewDelegate = self
bar.show(inSuperview: superview, withMessage: message)
}
/// Hide the message bar if it's currently shown.
func hide() {
hideTimer?.cancel()
toolbar?.hide({})
}
func listenForKeyboard() {
}
private var toolbar: DodoToolbar? {
get {
return superview.subviews.filter { $0 is DodoToolbar }.map { $0 as! DodoToolbar }.first
}
}
private func removeExistingBars() {
for view in superview.subviews {
if let existingToolbar = view as? DodoToolbar {
existingToolbar.removeFromSuperview()
}
}
}
// MARK: - Hiding after delay
private func setupHideTimer() {
hideTimer?.cancel()
if style.bar.hideAfterDelaySeconds > 0 {
hideTimer = MoaTimer.runAfter(style.bar.hideAfterDelaySeconds) { [weak self] timer in
DispatchQueue.main.async {
self?.hide()
}
}
}
}
// MARK: - Reacting to tap
private func setupHideOnTap(_ toolbar: UIView) {
onTap = OnTap(view: toolbar, gesture: UITapGestureRecognizer()) { [weak self] in
self?.didTapTheBar()
}
}
/// The bar has been tapped
private func didTapTheBar() {
style.bar.onTap?()
if style.bar.hideOnTap {
hide()
}
}
// MARK: - DodoButtonViewDelegate
func buttonDelegateDidTap(_ buttonStyle: DodoButtonStyle) {
if buttonStyle.hideOnTap {
hide()
}
}
}
// ----------------------------
//
// DodoMockMessage.swift
//
// ----------------------------
/**
Contains information about the message that was displayed in message bar. Used in unit tests.
*/
struct DodoMockMessage {
let preset: DodoPresets
let message: String
}
// ----------------------------
//
// DodoMock.swift
//
// ----------------------------
import UIKit
/**
This class is for testing the code that uses Dodo. It helps verifying the messages that were shown in the message bar without actually showing them.
Here is how to use it in your unit test.
1. Create an instance of DodoMock.
2. Set it to the `view.dodo` property of the view.
3. Run the code that you are testing.
4. Finally, verify which messages were shown in the message bar.
Example:
// Supply mock to the view
let dodoMock = DodoMock()
view.dodo = dodoMock
// Run the code from the app
runSomeAppCode()
// Verify the message is visible
XCTAssert(dodoMock.results.visible)
// Check total number of messages shown
XCTAssertEqual(1, dodoMock.results.total)
// Verify the text of the success message
XCTAssertEqual("To be prepared is half the victory.", dodoMock.results.success[0])
*/
public class DodoMock: DodoInterface {
/// This property is used in unit tests to verify which messages were displayed in the message bar.
public var results = DodoMockResults()
/**
Specify optional anchor for positioning the bar view.
This can be an anchor from the safe area.
*/
public var topAnchor: NSLayoutYAxisAnchor?
/**
Specify optional anchor for positioning the bar view.
This can be an anchor from the safe area.
*/
public var bottomAnchor: NSLayoutYAxisAnchor?
/// Specify optional layout guide for positioning the bar view. Deprecated, use bottomAnchor instead.
@available(*, deprecated, message: "Use topAnchor instead")
public var topLayoutGuide: UILayoutSupport? {
set { self.topAnchor = newValue?.bottomAnchor }
get { return nil }
}
/// Specify optional layout guide for positioning the bar view. Deprecated, use bottomAnchor instead.
@available(*, deprecated, message: "Use bottomAnchor instead")
public var bottomLayoutGuide: UILayoutSupport? {
set { self.topAnchor = newValue?.bottomAnchor }
get { return nil }
}
/// Defines styles for the bar.
public var style = DodoStyle(parentStyle: DodoPresets.defaultPreset.style)
/// Creates an instance of DodoMock class
public init() { }
/// Changes the style preset for the bar widget.
public var preset: DodoPresets = DodoPresets.defaultPreset {
didSet {
if preset != oldValue {
style.parent = preset.style
}
}
}
/**
Shows the message bar with *.success* preset. It can be used to indicate successful completion of an operation.
- parameter message: The text message to be shown.
*/
public func success(_ message: String) {
preset = .success
show(message)
}
/**
Shows the message bar with *.Info* preset. It can be used for showing information messages that have neutral emotional value.
- parameter message: The text message to be shown.
*/
public func info(_ message: String) {
preset = .info
show(message)
}
/**
Shows the message bar with *.warning* preset. It can be used for for showing warning messages.
- parameter message: The text message to be shown.
*/
public func warning(_ message: String) {
preset = .warning
show(message)
}
/**
Shows the message bar with *.warning* preset. It can be used for showing critical error messages
- parameter message: The text message to be shown.
*/
public func error(_ message: String) {
preset = .error
show(message)
}
/**
Shows the message bar. Set `preset` property to change the appearance of the message bar, or use the shortcut methods: `success`, `info`, `warning` and `error`.
- parameter message: The text message to be shown.
*/
public func show(_ message: String) {
let mockMessage = DodoMockMessage(preset: preset, message: message)
results.messages.append(mockMessage)
results.visible = true
}
/// Hide the message bar if it's currently shown.
public func hide() {
results.visible = false
}
}
// ----------------------------
//
// DodoMockResults.swift
//
// ----------------------------
/**
Used in unit tests to verify the messages that were shown in the message bar.
*/
public struct DodoMockResults {
/// An array of success messages displayed in the message bar.
public var success: [String] {
return messages.filter({ $0.preset == DodoPresets.success }).map({ $0.message })
}
/// An array of information messages displayed in the message bar.
public var info: [String] {
return messages.filter({ $0.preset == DodoPresets.info }).map({ $0.message })
}
/// An array of warning messages displayed in the message bar.
public var warning: [String] {
return messages.filter({ $0.preset == DodoPresets.warning }).map({ $0.message })
}
/// An array of error messages displayed in the message bar.
public var errors: [String] {
return messages.filter({ $0.preset == DodoPresets.error }).map({ $0.message })
}
/// Total number of messages shown.
public var total: Int {
return messages.count
}
/// Indicates whether the message is visible
public var visible = false
var messages = [DodoMockMessage]()
}
// ----------------------------
//
// DodoAnimationsHide.swift
//
// ----------------------------
import UIKit
/// Collection of animation effects use for hiding the notification bar.
struct DodoAnimationsHide {
/**
Animation that rotates the bar around X axis in perspective with spring effect.
- parameter view: View supplied for animation.
- parameter completed: A closure to be called after animation completes.
*/
static func rotate(_ view: UIView, duration: TimeInterval?, locationTop: Bool,
completed: @escaping DodoAnimationCompleted) {
DodoAnimations.doRotate(duration, showView: false, view: view, completed: completed)
}
/**
Animation that swipes the bar from to the left with fade-in effect.
- parameter view: View supplied for animation.
- parameter completed: A closure to be called after animation completes.
*/
static func slideLeft(_ view: UIView, duration: TimeInterval?, locationTop: Bool,
completed: @escaping DodoAnimationCompleted) {
DodoAnimations.doSlide(duration, right: false, showView: false, view: view, completed: completed)
}
/**
Animation that swipes the bar to the right with fade-out effect.
- parameter view: View supplied for animation.
- parameter completed: A closure to be called after animation completes.
*/
static func slideRight(_ view: UIView, duration: TimeInterval?, locationTop: Bool,
completed: @escaping DodoAnimationCompleted) {
DodoAnimations.doSlide(duration, right: true, showView: false, view: view, completed: completed)
}
/**
Animation that fades the bar out.
- parameter view: View supplied for animation.
- parameter completed: A closure to be called after animation completes.
*/
static func fade(_ view: UIView, duration: TimeInterval?, locationTop: Bool,
completed: @escaping DodoAnimationCompleted) {
DodoAnimations.doFade(duration, showView: false, view: view, completed: completed)
}
/**
Animation that slides the bar vertically out of view.
- parameter view: View supplied for animation.
- parameter completed: A closure to be called after animation completes.
*/
static func slideVertically(_ view: UIView, duration: TimeInterval?, locationTop: Bool,
completed: @escaping DodoAnimationCompleted) {
DodoAnimations.doSlideVertically(duration, showView: false, view: view,
locationTop: locationTop, completed: completed)
}
}
// ----------------------------
//
// DodoAnimations.swift
//
// ----------------------------
import UIKit
/// Collection of animation effects use for showing and hiding the notification bar.
public enum DodoAnimations: String {
/// Animation that fades the bar in/out.
case fade = "Fade"
/// Used for showing notification without animation.
case noAnimation = "No animation"
/// Animation that rotates the bar around X axis in perspective with spring effect.
case rotate = "Rotate"
/// Animation that swipes the bar to/from the left with fade effect.
case slideLeft = "Slide left"
/// Animation that swipes the bar to/from the right with fade effect.
case slideRight = "Slide right"
/// Animation that slides the bar in/out vertically.
case slideVertically = "Slide vertically"
/**
Get animation function that can be used for showing notification bar.
- returns: Animation function.
*/
public var show: DodoAnimation {
switch self {
case .fade:
return DodoAnimationsShow.fade
case .noAnimation:
return DodoAnimations.doNoAnimation
case .rotate:
return DodoAnimationsShow.rotate
case .slideLeft:
return DodoAnimationsShow.slideLeft
case .slideRight:
return DodoAnimationsShow.slideRight
case .slideVertically:
return DodoAnimationsShow.slideVertically
}
}
/**
Get animation function that can be used for hiding notification bar.
- returns: Animation function.
*/
public var hide: DodoAnimation {
switch self {
case .fade:
return DodoAnimationsHide.fade
case .noAnimation:
return DodoAnimations.doNoAnimation
case .rotate:
return DodoAnimationsHide.rotate
case .slideLeft:
return DodoAnimationsHide.slideLeft
case .slideRight:
return DodoAnimationsHide.slideRight
case .slideVertically:
return DodoAnimationsHide.slideVertically
}
}
/**
A empty animator which is used when no animation is supplied.
It simply calls the completion closure.
- parameter view: View supplied for animation.
- parameter completed: A closure to be called after animation completes.
*/
static func doNoAnimation(_ view: UIView, duration: TimeInterval?, locationTop: Bool,
completed: DodoAnimationCompleted) {
completed()
}
/// Helper function for fading the view in and out.
static func doFade(_ duration: TimeInterval?, showView: Bool, view: UIView,
completed: @escaping DodoAnimationCompleted) {
let actualDuration = duration ?? 0.5
let startAlpha: CGFloat = showView ? 0 : 1
let endAlpha: CGFloat = showView ? 1 : 0
view.alpha = startAlpha
UIView.animate(withDuration: actualDuration,
animations: {
view.alpha = endAlpha
},
completion: { finished in
completed()
}
)
}
/// Helper function for sliding the view vertically
static func doSlideVertically(_ duration: TimeInterval?, showView: Bool, view: UIView,
locationTop: Bool, completed: @escaping DodoAnimationCompleted) {
let actualDuration = duration ?? 0.5
view.layoutIfNeeded()
var distance: CGFloat = 0
if locationTop {
distance = view.frame.height + view.frame.origin.y
} else {
distance = UIScreen.main.bounds.height - view.frame.origin.y
}
let transform = CGAffineTransform(translationX: 0, y: locationTop ? -distance : distance)
let start: CGAffineTransform = showView ? transform : CGAffineTransform.identity
let end: CGAffineTransform = showView ? CGAffineTransform.identity : transform
view.transform = start
UIView.animate(withDuration: actualDuration,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 1,
options: [],
animations: {
view.transform = end
},
completion: { finished in
completed()
}
)
}
static weak var timer: MoaTimer?
/// Animation that rotates the bar around X axis in perspective with spring effect.
static func doRotate(_ duration: TimeInterval?, showView: Bool, view: UIView, completed: @escaping DodoAnimationCompleted) {
let actualDuration = duration ?? 2.0
let start: Double = showView ? Double(Double.pi / 2) : 0
let end: Double = showView ? 0 : Double(Double.pi / 2)
let damping = showView ? 0.85 : 3
let myCALayer = view.layer
var transform = CATransform3DIdentity
transform.m34 = -1.0/200.0
myCALayer.transform = CATransform3DRotate(transform, CGFloat(end), 1, 0, 0)
myCALayer.zPosition = 300
SpringAnimationCALayer.animate(myCALayer,
keypath: "transform.rotation.x",
duration: actualDuration,
usingSpringWithDamping: damping,
initialSpringVelocity: 1,
fromValue: start,
toValue: end,
onFinished: showView ? completed : nil)
// Hide the bar prematurely for better looks
timer?.cancel()
if !showView {
timer = MoaTimer.runAfter(0.3) { timer in
completed()
}
}
}
/// Animation that swipes the bar to the right with fade-out effect.
static func doSlide(_ duration: TimeInterval?, right: Bool, showView: Bool,
view: UIView, completed: @escaping DodoAnimationCompleted) {
let actualDuration = duration ?? 0.4
let distance = UIScreen.main.bounds.width
let transform = CGAffineTransform(translationX: right ? distance : -distance, y: 0)
let start: CGAffineTransform = showView ? transform : CGAffineTransform.identity
let end: CGAffineTransform = showView ? CGAffineTransform.identity : transform
let alphaStart: CGFloat = showView ? 0.2 : 1
let alphaEnd: CGFloat = showView ? 1 : 0.2
view.transform = start
view.alpha = alphaStart
UIView.animate(withDuration: actualDuration,
delay: 0,
options: UIView.AnimationOptions.curveEaseOut,
animations: {
view.transform = end
view.alpha = alphaEnd
},
completion: { finished in
completed()
}
)
}
}
// ----------------------------
//
// DodoAnimationsShow.swift
//
// ----------------------------
import UIKit
/// Collection of animation effects use for showing the notification bar.
struct DodoAnimationsShow {
/**
Animation that rotates the bar around X axis in perspective with spring effect.
- parameter view: View supplied for animation.
- parameter completed: A closure to be called after animation completes.
*/
static func rotate(_ view: UIView, duration: TimeInterval?,