-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAlertViewModel.swift
More file actions
executable file
·76 lines (66 loc) · 2.34 KB
/
AlertViewModel.swift
File metadata and controls
executable file
·76 lines (66 loc) · 2.34 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
//
// AlertViewModel.swift
// CustomAlertView
//
// Created by Shantaram Kokate on 9/11/18.
// Copyright © 2018 Shantaram Kokate. All rights reserved.
import Foundation
import UIKit
protocol AlertViewModel {
func show(animated: Bool)
func dismiss(animated: Bool)
var backgroundView: UIView {get set}
var containerView: UIView {get set}
}
enum AnimationOption: Int {
case zoomInOut
}
extension AlertViewModel where Self: UIView {
func show(animated: Bool) {
self.backgroundView.alpha = 0
if var topController = UIApplication.shared.delegate?.window??.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
let subviewsList = topController.view.subviews
for view in subviewsList where view is AlertView {
view.removeFromSuperview()
break
}
topController.view.addSubview(self)
}
/* if let topController = UIApplication.topViewController() {
let subviewsList = topController.view.subviews
for view in subviewsList where view is AlertView {
view.removeFromSuperview()
break
}
topController.view.addSubview(self)
} */
if animated {
self.containerView.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
UIView.animate(withDuration: 0.25, animations: {
self.backgroundView.alpha = 1.0
self.containerView.transform = .identity
}, completion: { (_) in
self.backgroundView.alpha = 1.0
})
}
}
func dismiss(animated: Bool) {
if animated {
self.backgroundView.alpha = 1.0
self.containerView.transform = .identity
UIView.animate(withDuration: 0.11, animations: {
self.backgroundView.alpha = 0.0
self.containerView.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
}, completion: { (_) in
self.backgroundView.alpha = 0.0
self.removeFromSuperview()
})
} else {
self.backgroundView.alpha = 0.0
self.removeFromSuperview()
}
}
}