forked from fengyu123123/QWWebViewFullScreenPopGesture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WKWebView+QWFullScreenPopGesture.m
159 lines (119 loc) · 5.8 KB
/
WKWebView+QWFullScreenPopGesture.m
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
//
// WKWebView+QWFullScreenPopGesture.m
// ProCalendar
//
// Created by 505 on 2018/3/21.
// Copyright © 2018年 Quickwis. All rights reserved.
//
#import "WKWebView+QWFullScreenPopGesture.h"
#import <objc/runtime.h>
//#import "NSObject+SXRuntime.h"
@interface _QWFullScreenPopGestureDelegate : NSObject <UIGestureRecognizerDelegate>
@property (weak,nonatomic) WKWebView * webView;
@end
@implementation _QWFullScreenPopGestureDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer{
//不可返回时忽略
if (![self.webView canGoBack]) {
return NO;
}
//禁止滑动手势时禁用
if (!self.webView.allowsBackForwardNavigationGestures) {
return NO;
}
//禁止水平向右以外的滑动
CGPoint translation = [gestureRecognizer translationInView:self.webView];
CGFloat absX = fabs(translation.x);
CGFloat absY = fabs(translation.y);
if (absX > absY ) { //水平滑动
if (translation.x<0) {
//向左滑动
return NO;
}
} else if (absX < absY ){//纵向滑动
return NO;
}
return YES;
}
@end
@implementation WKWebView (QWFullScreenPopGesture)
+ (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self swizzleInstanceMethodWithOriginSel:@selector(loadRequest:)
swizzledSel:@selector(qw_loadRequest:)];
[self swizzleInstanceMethodWithOriginSel:@selector(loadHTMLString:baseURL:)
swizzledSel:@selector(qw_loadHTMLString:baseURL:)];
if ([UIDevice currentDevice].systemVersion.floatValue >= 9.0) {
[self swizzleInstanceMethodWithOriginSel:@selector(loadFileURL:allowingReadAccessToURL:)
swizzledSel:@selector(qw_loadFileURL:allowingReadAccessToURL:)];
[self swizzleInstanceMethodWithOriginSel:@selector(loadData:MIMEType:characterEncodingName:baseURL:)
swizzledSel:@selector(qw_loadData:MIMEType:characterEncodingName:baseURL:)];
}
});
}
+ (void)swizzleInstanceMethodWithOriginSel:(SEL)originalSelector swizzledSel:(SEL)swizzledSelector{
Class class = [self class];
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (success) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
- (WKNavigation *)qw_loadRequest:(NSURLRequest *)request{
[self checkGestureIsAdded];
return [self qw_loadRequest:request];
}
- (WKNavigation *)qw_loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL{
[self checkGestureIsAdded];
return [self qw_loadHTMLString:string baseURL:baseURL];
}
- (WKNavigation *)qw_loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL{
[self checkGestureIsAdded];
return [self qw_loadFileURL:URL allowingReadAccessToURL:readAccessURL];
}
- (WKNavigation *)qw_loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL{
[self checkGestureIsAdded];
return [self qw_loadData:data MIMEType:MIMEType characterEncodingName:characterEncodingName baseURL:baseURL];
}
- (void)checkGestureIsAdded{
if (![self.gestureRecognizers containsObject:self.qw_fullscreenPopGestureRecognizer]) {
//self.gestureRecognizers一共两个手势
//firstObject是向右滑动返回的手势(UIScreenEdgePanGesture),lastObject是向左滑动前进的手势
UIPanGestureRecognizer * originEdgeRecognizer = self.gestureRecognizers.firstObject;
// Add our own gesture recognizer to where the onboard screen edge pan gesture recognizer is attached to.
[originEdgeRecognizer.view addGestureRecognizer:self.qw_fullscreenPopGestureRecognizer];
// Forward the gesture events to the private handler of the onboard gesture recognizer.
NSArray *internalTargets = [originEdgeRecognizer valueForKey:@"_targets"];
id internalTarget = [internalTargets.firstObject valueForKey:@"target"];
SEL internalAction = NSSelectorFromString(@"handleNavigationTransition:");
if (originEdgeRecognizer.delegate) {
self.qw_fullscreenPopGestureRecognizer.delegate = self.qw_popGestureRecognizerDelegate;
}
[self.qw_fullscreenPopGestureRecognizer addTarget:internalTarget action:internalAction];
// Disable the onboard gesture recognizer.
originEdgeRecognizer.enabled = NO;
}
}
- (UIPanGestureRecognizer *)qw_fullscreenPopGestureRecognizer{
UIPanGestureRecognizer *panGestureRecognizer = objc_getAssociatedObject(self, _cmd);
if (!panGestureRecognizer) {
panGestureRecognizer = [[UIPanGestureRecognizer alloc] init];
panGestureRecognizer.maximumNumberOfTouches = 1;
objc_setAssociatedObject(self, _cmd, panGestureRecognizer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return panGestureRecognizer;
}
- (_QWFullScreenPopGestureDelegate *)qw_popGestureRecognizerDelegate{
_QWFullScreenPopGestureDelegate *delegate = objc_getAssociatedObject(self, _cmd);
if (!delegate) {
delegate = [[_QWFullScreenPopGestureDelegate alloc] init];
delegate.webView = self;
objc_setAssociatedObject(self, _cmd, delegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return delegate;
}
@end