-
Notifications
You must be signed in to change notification settings - Fork 44
/
PresentationController+Setup.swift
179 lines (154 loc) · 7.02 KB
/
PresentationController+Setup.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
import UIKit
extension PresentationController {
func setupPresentationContainerView() {
guard self.presentationContainerView == nil else { return }
let presentationContainerView = PresentationContainerView()
presentationContainerView.touchDelegate = self
presentationContainerView.backgroundColor = .clear
presentationContainerView.frame = containerView?.superview?.frame ?? .zero
presentationContainerView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
if let containerView = containerView {
containerView.superview?.addSubview(presentationContainerView)
presentationContainerView.addSubview(containerView)
}
self.presentationContainerView = presentationContainerView
}
func removePresentationContainerView() {
presentationContainerView.removeFromSuperview()
}
}
extension PresentationController {
func setupDrawerFullExpansionTapRecogniser() {
guard drawerFullExpansionTapGR == nil else { return }
let isFullyPresentable = isFullyPresentableByDrawerTaps
let numTapsRequired = numberOfTapsForFullDrawerPresentation
guard isFullyPresentable && numTapsRequired > 0 else { return }
let tapGesture = UITapGestureRecognizer(target: self,
action: #selector(handleDrawerFullExpansionTap))
tapGesture.numberOfTouchesRequired = 1
tapGesture.numberOfTapsRequired = numTapsRequired
tapGesture.cancelsTouchesInView = false
tapGesture.delaysTouchesBegan = false
tapGesture.delaysTouchesEnded = false
tapGesture.delegate = self
presentedView?.addGestureRecognizer(tapGesture)
drawerFullExpansionTapGR = tapGesture
}
func removeDrawerFullExpansionTapRecogniser() {
guard let tapGesture = drawerFullExpansionTapGR else { return }
presentedView?.removeGestureRecognizer(tapGesture)
drawerFullExpansionTapGR = nil
}
func enableDrawerFullExpansionTapRecogniser(enabled: Bool) {
drawerFullExpansionTapGR?.isEnabled = enabled
}
}
extension PresentationController {
func setupDrawerDismissalTapRecogniser() {
guard drawerDismissalTapGR == nil else { return }
let isDismissable = isDismissableByOutsideDrawerTaps
let numTapsRequired = numberOfTapsForOutsideDrawerDismissal
guard isDismissable && numTapsRequired > 0 else { return }
let tapGesture = UITapGestureRecognizer(target: self,
action: #selector(handleDrawerDismissalTap))
tapGesture.numberOfTouchesRequired = 1
tapGesture.numberOfTapsRequired = numTapsRequired
tapGesture.cancelsTouchesInView = false
tapGesture.delaysTouchesBegan = false
tapGesture.delaysTouchesEnded = false
tapGesture.delegate = self
containerView?.addGestureRecognizer(tapGesture)
drawerDismissalTapGR = tapGesture
}
func removeDrawerDismissalTapRecogniser() {
guard let tapGesture = drawerDismissalTapGR else { return }
containerView?.removeGestureRecognizer(tapGesture)
drawerDismissalTapGR = nil
}
func enableDrawerDismissalTapRecogniser(enabled: Bool) {
drawerDismissalTapGR?.isEnabled = enabled
}
}
extension PresentationController {
func setupDrawerDragRecogniser() {
guard drawerDragGR == nil, isDrawerDraggable else { return }
let panGesture = UIPanGestureRecognizer(target: self,
action: #selector(handleDrawerDrag))
presentedView?.addGestureRecognizer(panGesture)
drawerDragGR = panGesture
}
func removeDrawerDragRecogniser() {
guard let panGesture = drawerDragGR else { return }
presentedView?.removeGestureRecognizer(panGesture)
drawerDragGR = nil
}
}
extension PresentationController {
func setupDrawerBorder() {
if let drawerBorderConfig = configuration.drawerBorderConfiguration {
presentedView?.layer.borderWidth = drawerBorderConfig.borderThickness
presentedView?.layer.borderColor = drawerBorderConfig.borderColor?.cgColor
}
}
func setupDrawerShadow() {
if let drawerShadowConfig = configuration.drawerShadowConfiguration {
presentedView?.layer.shadowColor = drawerShadowConfig.shadowColor?.cgColor
presentedView?.layer.shadowOpacity = Float(drawerShadowConfig.shadowOpacity)
presentedView?.layer.shadowRadius = drawerShadowConfig.shadowRadius
presentedView?.layer.shadowOffset = drawerShadowConfig.shadowOffset
}
}
}
extension PresentationController {
func setupHandleView() {
guard
let presentedView = self.presentedView,
let handleView = self.handleView,
let handleConfig = configuration.handleViewConfiguration
else { return }
handleView.translatesAutoresizingMaskIntoConstraints = false
handleView.backgroundColor = handleConfig.backgroundColor
handleView.layer.masksToBounds = true
switch handleConfig.cornerRadius {
case .automatic:
handleView.layer.cornerRadius = handleConfig.size.height / 2
case let .custom(radius):
handleView.layer.cornerRadius = radius
}
presentedView.addSubview(handleView)
NSLayoutConstraint.activate([
handleView.widthAnchor.constraint(equalToConstant: handleConfig.size.width),
handleView.heightAnchor.constraint(equalToConstant: handleConfig.size.height),
handleView.centerXAnchor.constraint(equalTo: presentedView.centerXAnchor),
handleView.topAnchor.constraint(equalTo: presentedView.topAnchor, constant: handleConfig.top)
])
}
func removeHandleView() {
self.handleView?.removeFromSuperview()
}
}
extension PresentationController {
func setupDebugHeightMarks() {
guard inDebugMode && (upperMarkGap > 0 || lowerMarkGap > 0),
let containerView = containerView else { return }
if upperMarkGap > 0 {
let upperMarkYView = UIView()
upperMarkYView.backgroundColor = .black
upperMarkYView.frame = CGRect(x: 0, y: upperMarkY,
width: containerView.bounds.size.width, height: 3)
containerView.addSubview(upperMarkYView)
}
if lowerMarkGap > 0 {
let lowerMarkYView = UIView()
lowerMarkYView.backgroundColor = .black
lowerMarkYView.frame = CGRect(x: 0, y: lowerMarkY,
width: containerView.bounds.size.width, height: 3)
containerView.addSubview(lowerMarkYView)
}
let drawerMarkView = UIView()
drawerMarkView.backgroundColor = .white
drawerMarkView.frame = CGRect(x: 0, y: drawerPartialY,
width: containerView.bounds.size.width, height: 3)
containerView.addSubview(drawerMarkView)
}
}