UIViewController+GKGesture.swift
8.2 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
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
//
// UIViewController+GKGesture.swift
// GKNavigationBarSwift
//
// Created by QuintGao on 2022/3/18.
// Copyright © 2022 QuintGao. All rights reserved.
//
import UIKit
// 左滑push代理
@objc public protocol GKViewControllerPushDelegate: NSObjectProtocol {
/// 左滑push,在这里创建将要push的控制器
@objc optional func pushToNextViewController()
/// push手势滑动开始
@objc optional func viewControllerPushScrollBegan()
/// push手势滑动进度更新
/// - Parameter progress: 进度(0-1)
@objc optional func viewControllerPushScrollUpdate(progress: CGFloat)
/// push手势滑动结束
/// - Parameter finished: 是否完成push操作(true:push成功 false:push取消)
@objc optional func viewControllerPushScrollEnded(finished: Bool)
}
// 右滑pop代理
@objc public protocol GKViewControllerPopDelegate: NSObjectProtocol {
/// pop手势滑动开始
@objc optional func viewControllerPopScrollBegan()
/// pop手势滑动进度更新
/// - Parameter progress: 进度(0-1)
@objc optional func viewControllerPopScrollUpdate(progress: CGFloat)
/// pop手势滑动结束
/// - Parameter finished: 是否完成pop操作(true:pop成功 false:pop取消)
@objc optional func viewControllerPopScrollEnded(finished: Bool)
}
// 返回拦截
@objc public protocol GKGesturePopHandlerProtocol: NSObjectProtocol {
/// 是否可以返回,包括点击返回和手势返回,默认YES
@objc func navigationShouldPop() -> Bool
/// 是否可以手势返回
@objc optional func navigationShouldPopOnGesture() -> Bool
/// 是否可以点击返回
@objc optional func navigationShouldPopOnClick() -> Bool
/// 返回手势冲突处理,当返回手势与其他手势冲突如:WKWebView中的手势,可实现以下方法返回YES,让返回手势与其他手势共存来解决手势冲突
@objc optional func popGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
}
public let GKViewControllerPropertyChanged = NSNotification.Name(rawValue: "GKViewControllerPropertyChanged")
extension UIViewController: GKGestureAwakeProtocol {
// MARK: - 重新系统方法
private static let onceToken = UUID().uuidString
@objc public static func gkGestureAwake() {
DispatchQueue.once(token: onceToken) {
let oriSels = ["viewWillAppear:",
"viewDidAppear:",
"viewDidDisappear:"]
for oriSel in oriSels {
gk_swizzled_instanceMethod("gkGesture", oldClass: self, oldSelector: oriSel, newClass: self)
}
}
}
@objc func gkGesture_viewWillAppear(_ animated: Bool) {
if self.hasPushDelegate {
self.gk_pushDelegate = self as? GKViewControllerPushDelegate
self.hasPushDelegate = false
}
if self.hasPopDelegate {
self.gk_popDelegate = self as? GKViewControllerPopDelegate
self.hasPopDelegate = false
}
gkGesture_viewWillAppear(animated)
}
@objc func gkGesture_viewDidAppear(_ animated: Bool) {
postPropertyChangeNotification()
gkGesture_viewDidAppear(animated)
}
@objc func gkGesture_viewDidDisappear(_ animated: Bool) {
if let delegate = self.gk_pushDelegate as? NSObject, delegate == self {
self.hasPushDelegate = true
}
if let delegate = self.gk_popDelegate as? NSObject, delegate == self {
self.hasPopDelegate = true
}
// 这两个代理系统不会自动回收,所以要做下处理
self.gk_pushDelegate = nil;
self.gk_popDelegate = nil;
gkGesture_viewDidDisappear(animated)
}
fileprivate func postPropertyChangeNotification() {
NotificationCenter.default.post(name: GKViewControllerPropertyChanged, object: ["viewController": self])
}
}
extension UIViewController {
fileprivate struct AssociatedKeys {
static var gkInteractivePopDisabled: Void?
static var gkFullScreenPopDisabled: Void?
static var gkSystemGestureHandleDisabled: Void?
static var gkMaxPopDistance: Void?
static var gkPushDelegate: Void?
static var gkPopDelegate: Void?
static var gkPushTransition: Void?
static var gkPopTransition: Void?
static var hasPushDelegate: Void?
static var hasPopDelegate: Void?
}
public var gk_interactivePopDisabled: Bool {
get {
guard let obj = gk_getAssociatedObject(self, &AssociatedKeys.gkInteractivePopDisabled) as? Bool else { return false }
return obj
}
set {
gk_setAssociatedObject(self, &AssociatedKeys.gkInteractivePopDisabled, newValue)
postPropertyChangeNotification()
}
}
public var gk_fullScreenPopDisabled: Bool {
get {
guard let obj = gk_getAssociatedObject(self, &AssociatedKeys.gkFullScreenPopDisabled) as? Bool else { return false }
return obj
}
set {
gk_setAssociatedObject(self, &AssociatedKeys.gkFullScreenPopDisabled, newValue)
postPropertyChangeNotification()
}
}
public var gk_systemGestureHandleDisabled: Bool {
get {
guard let obj = gk_getAssociatedObject(self, &AssociatedKeys.gkSystemGestureHandleDisabled) as? Bool else { return false }
return obj
}
set {
gk_setAssociatedObject(self, &AssociatedKeys.gkSystemGestureHandleDisabled, newValue)
postPropertyChangeNotification()
}
}
public var gk_maxPopDistance: CGFloat {
get {
guard let obj = gk_getAssociatedObject(self, &AssociatedKeys.gkMaxPopDistance) as? CGFloat else { return 0 }
return obj
}
set {
gk_setAssociatedObject(self, &AssociatedKeys.gkMaxPopDistance, newValue)
postPropertyChangeNotification()
}
}
public var gk_pushDelegate: GKViewControllerPushDelegate? {
get {
return gk_getAssociatedObject(self, &AssociatedKeys.gkPushDelegate) as? GKViewControllerPushDelegate
}
set {
gk_setAssociatedObject(self, &AssociatedKeys.gkPushDelegate, newValue)
}
}
public var gk_popDelegate: GKViewControllerPopDelegate? {
get {
return gk_getAssociatedObject(self, &AssociatedKeys.gkPopDelegate) as? GKViewControllerPopDelegate
}
set {
gk_setAssociatedObject(self, &AssociatedKeys.gkPopDelegate, newValue)
}
}
public var gk_pushTransition: UIViewControllerAnimatedTransitioning? {
get {
return gk_getAssociatedObject(self, &AssociatedKeys.gkPushTransition) as? UIViewControllerAnimatedTransitioning
}
set {
gk_setAssociatedObject(self, &AssociatedKeys.gkPushTransition, newValue)
}
}
public var gk_popTransition: UIViewControllerAnimatedTransitioning? {
get {
return gk_getAssociatedObject(self, &AssociatedKeys.gkPopTransition) as? UIViewControllerAnimatedTransitioning
}
set {
gk_setAssociatedObject(self, &AssociatedKeys.gkPopTransition, newValue)
}
}
fileprivate var hasPushDelegate: Bool {
get {
guard let obj = gk_getAssociatedObject(self, &AssociatedKeys.hasPushDelegate) as? Bool else { return false }
return obj
}
set {
gk_setAssociatedObject(self, &AssociatedKeys.hasPushDelegate, newValue)
}
}
fileprivate var hasPopDelegate: Bool {
get {
guard let obj = gk_getAssociatedObject(self, &AssociatedKeys.hasPopDelegate) as? Bool else { return false }
return obj
}
set {
gk_setAssociatedObject(self, &AssociatedKeys.hasPopDelegate, newValue)
}
}
}
extension UIViewController: GKGesturePopHandlerProtocol {
open func navigationShouldPop() -> Bool {
return true
}
}