-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathPullToDismiss.swift
259 lines (222 loc) · 9.13 KB
/
PullToDismiss.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
//
// PullToDismiss.swift
// PullToDismiss
//
// Created by Suguru Kishimoto on 11/13/16.
// Copyright © 2016 Suguru Kishimoto. All rights reserved.
//
import Foundation
import UIKit
open class PullToDismiss: NSObject {
public struct Defaults {
private init() {}
public static let dismissableHeightPercentage: CGFloat = 0.33
}
open var backgroundEffect: BackgroundEffect? = ShadowEffect.default
open var edgeShadow: EdgeShadow? = EdgeShadow.default
public var dismissAction: (() -> Void)?
public weak var delegate: UIScrollViewDelegate? {
didSet {
var delegates: [UIScrollViewDelegate] = [self]
if let delegate = delegate {
delegates.append(delegate)
}
proxy = ScrollViewDelegateProxy(delegates: delegates)
}
}
public var dismissableHeightPercentage: CGFloat = Defaults.dismissableHeightPercentage {
didSet {
dismissableHeightPercentage = min(max(0.0, dismissableHeightPercentage), 1.0)
}
}
fileprivate var viewPositionY: CGFloat = 0.0
fileprivate var dragging: Bool = false
fileprivate var previousContentOffsetY: CGFloat = 0.0
fileprivate weak var viewController: UIViewController?
private var __scrollView: UIScrollView?
private var proxy: ScrollViewDelegateProxy? {
didSet {
__scrollView?.delegate = proxy
}
}
private var panGesture: UIPanGestureRecognizer?
private var backgroundView: UIView?
private var navigationBarHeight: CGFloat = 0.0
private var blurSaturationDeltaFactor: CGFloat = 1.8
convenience public init?(scrollView: UIScrollView) {
guard let viewController = type(of: self).viewControllerFromScrollView(scrollView) else {
print("a scrollView must be on the view controller.")
return nil
}
self.init(scrollView: scrollView, viewController: viewController)
}
public init(scrollView: UIScrollView, viewController: UIViewController, navigationBar: UIView? = nil) {
super.init()
self.proxy = ScrollViewDelegateProxy(delegates: [self])
self.__scrollView = scrollView
self.__scrollView?.delegate = self.proxy
self.viewController = viewController
if let navigationBar = navigationBar ?? viewController.navigationController?.navigationBar {
let gesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
navigationBar.addGestureRecognizer(gesture)
self.navigationBarHeight = navigationBar.frame.height
self.panGesture = gesture
}
}
deinit {
if let panGesture = panGesture {
panGesture.view?.removeGestureRecognizer(panGesture)
}
proxy = nil
__scrollView?.delegate = nil
__scrollView = nil
}
fileprivate var targetViewController: UIViewController? {
return viewController?.navigationController ?? viewController
}
private var haveShadowEffect: Bool {
return backgroundEffect != nil || edgeShadow != nil
}
fileprivate func dismiss() {
targetViewController?.dismiss(animated: true, completion: nil)
}
// MARK: - shadow view
private func makeBackgroundView() {
deleteBackgroundView()
guard let backgroundEffect = backgroundEffect else {
return
}
let backgroundView = backgroundEffect.makeBackgroundView()
backgroundView.frame = targetViewController?.view.bounds ?? .zero
switch backgroundEffect.target {
case .targetViewController:
targetViewController?.view.addSubview(backgroundView)
backgroundView.superview?.sendSubviewToBack(backgroundView)
backgroundView.frame = targetViewController?.view.bounds ?? .zero
case .presentingViewController:
targetViewController?.presentingViewController?.view.addSubview(backgroundView)
backgroundView.frame = targetViewController?.presentingViewController?.view.bounds ?? .zero
}
self.backgroundView = backgroundView
}
private func updateBackgroundView(rate: CGFloat) {
guard let backgroundEffect = backgroundEffect else {
return
}
backgroundEffect.applyEffect(view: backgroundView, rate: rate)
}
private func deleteBackgroundView() {
backgroundView?.removeFromSuperview()
backgroundView = nil
targetViewController?.view.clipsToBounds = true
}
private func resetBackgroundView() {
guard let backgroundEffect = backgroundEffect else {
return
}
backgroundEffect.applyEffect(view: backgroundView, rate: 1.0)
}
@objc private func handlePanGesture(_ gesture: UIPanGestureRecognizer) {
switch gesture.state {
case .began:
startDragging()
case .changed:
let diff = gesture.translation(in: gesture.view).y
updateViewPosition(offset: diff)
gesture.setTranslation(.zero, in: gesture.view)
case .ended:
finishDragging(withVelocity: .zero)
default:
break
}
}
fileprivate func startDragging() {
targetViewController?.view.layer.removeAllAnimations()
backgroundView?.layer.removeAllAnimations()
viewPositionY = 0.0
makeBackgroundView()
targetViewController?.view.applyEdgeShadow(edgeShadow)
if haveShadowEffect {
targetViewController?.view.clipsToBounds = false
}
}
fileprivate func updateViewPosition(offset: CGFloat) {
var addOffset: CGFloat = offset
// avoid statusbar gone
if viewPositionY >= 0 && viewPositionY < 0.05 {
addOffset = min(max(-0.01, addOffset), 0.01)
}
viewPositionY += addOffset
targetViewController?.view.frame.origin.y = max(0.0, viewPositionY)
if case .some(.targetViewController) = backgroundEffect?.target {
backgroundView?.frame.origin.y = -(targetViewController?.view.frame.origin.y ?? 0.0)
}
let targetViewOriginY: CGFloat = targetViewController?.view.frame.origin.y ?? 0.0
let targetViewHeight: CGFloat = targetViewController?.view.frame.height ?? 0.0
let rate: CGFloat = (1.0 - (targetViewOriginY / (targetViewHeight * dismissableHeightPercentage)))
updateBackgroundView(rate: rate)
targetViewController?.view.updateEdgeShadow(edgeShadow, rate: rate)
}
fileprivate func finishDragging(withVelocity velocity: CGPoint) {
let originY = targetViewController?.view.frame.origin.y ?? 0.0
let dismissableHeight = (targetViewController?.view.frame.height ?? 0.0) * dismissableHeightPercentage
if originY > dismissableHeight || originY > 0 && velocity.y < 0 {
deleteBackgroundView()
targetViewController?.view.detachEdgeShadow()
proxy = nil
_ = dismissAction?() ?? dismiss()
} else if originY != 0.0 {
UIView.perform(.delete, on: [], options: [.allowUserInteraction], animations: { [weak self] in
self?.targetViewController?.view.frame.origin.y = 0.0
self?.resetBackgroundView()
self?.targetViewController?.view.updateEdgeShadow(self?.edgeShadow, rate: 1.0)
}) { [weak self] finished in
if finished {
self?.deleteBackgroundView()
self?.targetViewController?.view.detachEdgeShadow()
}
}
} else {
self.deleteBackgroundView()
}
viewPositionY = 0.0
}
private static func viewControllerFromScrollView(_ scrollView: UIScrollView) -> UIViewController? {
var responder: UIResponder? = scrollView
while let r = responder {
if let viewController = r as? UIViewController {
return viewController
}
responder = r.next
}
return nil
}
}
extension PullToDismiss: UITableViewDelegate {
}
extension PullToDismiss: UICollectionViewDelegate {
}
extension PullToDismiss: UICollectionViewDelegateFlowLayout {
}
extension PullToDismiss: UIScrollViewDelegate {
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
if dragging {
let diff = -(scrollView.contentOffset.y - previousContentOffsetY)
if scrollView.contentOffset.y < -scrollView.contentInset.top || (targetViewController?.view.frame.origin.y ?? 0.0) > 0.0 {
updateViewPosition(offset: diff)
scrollView.contentOffset.y = -scrollView.contentInset.top
}
previousContentOffsetY = scrollView.contentOffset.y
}
}
public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
startDragging()
dragging = true
previousContentOffsetY = scrollView.contentOffset.y
}
public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
finishDragging(withVelocity: velocity)
dragging = false
previousContentOffsetY = 0.0
}
}