-
Notifications
You must be signed in to change notification settings - Fork 0
/
InformationViewManager.swift
174 lines (156 loc) · 7.39 KB
/
InformationViewManager.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
//
// InformationViewManager.swift
//
// Created by N A Shashank on 21/04/21.
// Copyright © 2021 Shashank. All rights reserved.
//
import UIKit
public struct InformationViewDataModel {
let image: UIImage
let description: String
let backgroundColor: UIColor
let titleColor: UIColor
let font: UIFont
public init(image: UIImage,
description: String,
backgroundColor: UIColor,
titleColor: UIColor,
font: UIFont) {
self.image = image
self.description = description
self.backgroundColor = backgroundColor
self.titleColor = titleColor
self.font = font
}
}
public final class InformationViewManager {
weak var infoView: UIView?
weak var parentView: UIView?
var bottomConstraint: NSLayoutConstraint?
private let dynamicAnimator: UIDynamicAnimator
public init(parentView: UIView) {
self.parentView = parentView
self.dynamicAnimator = UIDynamicAnimator(referenceView: parentView)
//nothing to do here
}
public func showUsing(dataModel: InformationViewDataModel, positionOffset: CGFloat = 0.0) {
guard self.infoView == nil,
let parentView = self.parentView else{
return
}
let containerView = UIView()
let imageView = UIImageView()
let label = UILabel()
label.numberOfLines = 0
containerView.layer.masksToBounds = true
containerView.layer.cornerRadius = CGFloat(8)
parentView.addSubview(containerView)
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.leadingAnchor.constraint(equalTo: parentView.leadingAnchor,
constant: 15).isActive = true
containerView.trailingAnchor.constraint(equalTo: parentView.trailingAnchor,
constant: -15).isActive = true
self.bottomConstraint = containerView.bottomAnchor.constraint(equalTo: parentView.bottomAnchor,
constant: -10 + positionOffset)
self.bottomConstraint?.isActive = true
containerView.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.widthAnchor.constraint(equalToConstant: 20).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 20).isActive = true
imageView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor,
constant: 15).isActive = true
imageView.topAnchor.constraint(equalTo: containerView.topAnchor,
constant: 15).isActive = true
containerView.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
label.leadingAnchor.constraint(equalTo: imageView.trailingAnchor,
constant: 5).isActive = true
label.topAnchor.constraint(equalTo: containerView.topAnchor,
constant: 15).isActive = true
label.trailingAnchor.constraint(equalTo: containerView.trailingAnchor,
constant: -15).isActive = true
label.bottomAnchor.constraint(equalTo: containerView.bottomAnchor,
constant: -13).isActive = true
label.heightAnchor.constraint(greaterThanOrEqualToConstant: 20).isActive = true
imageView.image = dataModel.image
label.textColor = dataModel.titleColor
label.text = dataModel.description
label.font = dataModel.font
containerView.backgroundColor = dataModel.backgroundColor
let pangesture = UIPanGestureRecognizer(target: self, action: #selector(self.handlePanGesture(gestureRecognizer:)))
containerView.addGestureRecognizer(pangesture)
self.infoView = containerView
self.animateAndDismissAlert()
}
@objc func handlePanGesture(gestureRecognizer: UIPanGestureRecognizer) {
guard let parentView = self.parentView,let softAlertView = self.infoView else{
return
}
switch gestureRecognizer.state {
case UIGestureRecognizer.State.began:
self.dynamicAnimator.removeAllBehaviors()
case UIGestureRecognizer.State.changed:
let translation = gestureRecognizer.translation(in: parentView)
self.bottomConstraint?.constant += translation.y
gestureRecognizer.setTranslation(CGPoint.zero, in: parentView)
let targetRect = CGRect(x: 0, y: parentView.bounds.height, width: parentView.bounds.width, height: 100)
guard softAlertView.frame.intersects(targetRect) else{
return
}
let pushBehavior = UIPushBehavior(items: [softAlertView], mode: .instantaneous)
pushBehavior.pushDirection = CGVector(dx: 0, dy: 1.0)
pushBehavior.magnitude = 0.5
self.dynamicAnimator.addBehavior(pushBehavior)
pushBehavior.action = {[weak self] in
guard let reqdView = self?.infoView, reqdView.frame.origin.y > parentView.bounds.height else{
return
}
reqdView.removeFromSuperview()
self?.infoView = nil
self?.dynamicAnimator.removeAllBehaviors()
}
case UIGestureRecognizer.State.ended:
let velocity = gestureRecognizer.velocity(in: parentView)
guard velocity.y > 200 else{
return
}
let pushBehavior = UIPushBehavior(items: [softAlertView], mode: .instantaneous)
pushBehavior.pushDirection = CGVector(dx: 0, dy: velocity.y)
pushBehavior.magnitude = 20
self.dynamicAnimator.addBehavior(pushBehavior)
pushBehavior.action = {[weak self] in
guard let reqdView = self?.infoView, reqdView.frame.origin.y > parentView.bounds.height else{
return
}
reqdView.removeFromSuperview()
self?.infoView = nil
self?.dynamicAnimator.removeAllBehaviors()
}
default:
print("no handler for this case")
}
}
private func animateAndDismissAlert() {
guard let alertView = self.infoView else{
assertionFailure("could not fetch soft alert instance")
return
}
alertView.alpha = 0
UIView.animate(withDuration: 1, delay: 0, options: .allowUserInteraction, animations: {[weak alertView] in
alertView?.alpha = 1
}, completion: nil)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {[weak self] in
guard let unwrappedSelf = self,
let alertView = unwrappedSelf.infoView else{
print("could not fetch soft alert instance")
return
}
UIView.animate(withDuration: 1, delay: 0, options: UIView.AnimationOptions.allowUserInteraction) {[weak alertView] in
alertView?.alpha = 0
} completion: {[weak self] (flag) in
self?.infoView?.removeFromSuperview()
self?.infoView = nil
}
}
}
}