-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathPanels.swift
201 lines (176 loc) · 8.45 KB
/
Panels.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
//
// Created by Antonio Casero Palmero on 10.08.18.
// Copyright © 2018 Panels. All rights reserved.
//
import UIKit
public class Panels {
public weak var delegate: PanelNotifications?
public var isExpanded: Bool {
return (panelHeightConstraint?.constant ?? 0.0) > configuration.visibleArea()
}
private weak var panel: (Panelable & UIViewController)?
private weak var parentViewController: UIViewController?
private weak var containerView: UIView?
private weak var panelHeightConstraint: NSLayoutConstraint?
private lazy var configuration: PanelConfiguration = PanelConfiguration()
private var panelHeight: CGFloat = 0.0
public init(target: UIViewController) {
parentViewController = target
}
/// Add a viewcontainer to the view target. This subview is the panel definded in
/// a storyboard and conform the protocol Panelable.
/// - Parameters:
/// - config: Configuration panel, there you can define the panel behaviour
/// - target: Viewcontroller where the panel will be added as subview.
/// - view: Alternative view to viewController.view
public func show(panel: Panelable & UIViewController,
config: PanelConfiguration = PanelConfiguration(),
view: UIView? = nil) {
assert(self.panel == nil, "You are trying to push a panel without dismiss the previous one.")
configuration = config
containerView = view ?? parentViewController?.view
self.panel = panel
parentViewController?.addContainer(container: panel)
guard let container = containerView else {
fatalError("No parent view available")
}
panelHeightConstraint = addChildToContainer(parent: container,
child: panel.view,
visible: config.visibleArea(),
size: config.size(for: container))
panel.hideKeyboardAutomatically()
registerKeyboardNotifications()
// Prepare the view placement, saving the safeArea.
panelHeight = config.heightConstant ?? panel.headerHeight.constant
panel.headerHeight.constant = panelHeight + UIApplication.safeAreaBottom()
setupGestures(headerView: panel.headerPanel, superview: container)
}
/// Opens the panel
@objc public func expandPanel() {
guard !isExpanded, let container = containerView else {
return
}
movePanel(value: configuration.size(for: container))
}
/// Close the panel
@objc public func collapsePanel() {
guard isExpanded, let container = containerView else {
return
}
movePanel(value: configuration.visibleArea())
container.endEditing(true)
}
public func dismiss(completion: (() -> Void)? = nil) {
panel?.headerHeight.constant = panelHeight
guard let panelView = panel?.view else {
completion?()
return
}
UIView.animate(withDuration: configuration.dismissAnimationDuration, animations: {
panelView.frame.origin = CGPoint(x: 0, y: self.containerView!.frame.size.height)
}) { _ in
self.panel?.removeContainer()
self.panel = nil
completion?()
}
}
deinit {
NotificationCenter.default.removeObserver(self)
}
}
// MARK: Private functions
extension Panels {
private func movePanel(value: CGFloat, keyboard: Bool = false, completion: (() -> Void)? = nil) {
panelHeightConstraint?.constant = value
if !keyboard {
panel?.headerHeight.constant += isExpanded ? -UIApplication.safeAreaBottom() : UIApplication.safeAreaBottom()
}
isExpanded ? delegate?.panelDidOpen() : delegate?.panelDidCollapse()
containerView?.animateLayoutBounce(completion: completion) ?? completion?()
}
private func addChildToContainer(parent container: UIView,
child childView: UIView,
visible: CGFloat,
size: CGFloat) -> NSLayoutConstraint {
childView.translatesAutoresizingMaskIntoConstraints = false
childView.frame = CGRect(x: 0,
y: container.bounds.maxY + configuration.visibleArea(),
width: container.bounds.width,
height: configuration.visibleArea())
let views = ["childView": childView]
let horizontalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|[childView]|",
options: NSLayoutConstraint.FormatOptions(rawValue: 0),
metrics: nil,
views: views)
container.addConstraints(horizontalConstraints)
let heightConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:[childView(==\(size))]",
options: NSLayoutConstraint.FormatOptions(rawValue: 0),
metrics: nil,
views: views)
container.addConstraints(heightConstraints)
let constraint = container.bottomAnchor.constraint(equalTo: childView.topAnchor,
constant: visible)
constraint.isActive = true
if configuration.animateEntry {
childView.animateLayoutBounce(duration: configuration.entryAnimationDuration)
} else {
childView.layoutIfNeeded()
}
delegate?.panelDidPresented()
return constraint
}
}
// MARK: Keyboard control
extension Panels {
private func registerKeyboardNotifications() {
if configuration.keyboardObserver {
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillShow(notification:)),
name: UIResponder.keyboardWillShowNotification,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillHide(notification:)),
name: UIResponder.keyboardWillHideNotification,
object: nil)
}
}
@objc private func keyboardWillShow(notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = CGFloat(keyboardRectangle.height)
containerView.then {
let currentValue = isExpanded ? configuration.size(for: $0) : configuration.visibleArea()
movePanel(value: currentValue + keyboardHeight, keyboard: true)
}
}
}
@objc private func keyboardWillHide(notification _: Notification) {
collapsePanel()
}
}
// MARK: Gesture control
extension Panels {
private func setupGestures(headerView: UIView, superview _: UIView) {
// Expand and collapse:
if configuration.respondToTap {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
headerView.addGestureRecognizer(tapGesture)
}
if configuration.respondToDrag {
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(expandPanel))
swipeUp.direction = .up
headerView.addGestureRecognizer(swipeUp)
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(collapsePanel))
swipeDown.direction = .down
headerView.addGestureRecognizer(swipeDown)
}
if configuration.closeOutsideTap {
let tapGestureOutside = UITapGestureRecognizer(target: self, action: #selector(collapsePanel))
tapGestureOutside.cancelsTouchesInView = false
containerView?.addGestureRecognizer(tapGestureOutside)
}
}
@objc private func handleTap() {
(isExpanded) ? collapsePanel() : expandPanel()
}
}