YHExplorerView.m
12.6 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//
// YHExplorerView.m
// YohoExplorerDemo
//
// Created by gaoqiang xu on 3/2/15.
// Copyright (c) 2015 gaoqiang xu. All rights reserved.
//
#import "YHExplorerView.h"
@interface YHExplorerView ()
<UIGestureRecognizerDelegate>
@property (strong, nonatomic) YHExplorerViewController *webViewController;
@property (strong, nonatomic) NSString *url;
@property (assign, nonatomic) BOOL isLoaded;
@property (strong, nonatomic) UITapGestureRecognizer *tapGesture;
@end
@implementation YHExplorerView
- (instancetype)initWithFrame:(CGRect)frame withNaviBar:(UINavigationBar *)naviBar
{
self = [self initWithFrame:frame];
self.currentNaviBar = naviBar;
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self _initialize];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self _initialize];
}
return self;
}
- (void)_initialize {
self.isLoaded = NO;
self.enablePictureTapGesture = NO;
}
- (void)setUrl:(NSString *)url
{
// 检验url是否能正常转成NSURL,防止webview不能加载
BOOL isValidUrl = ([NSURL URLWithString:url] != nil);
if (url.length > 0
&& !isValidUrl) {
// 如果url里面含有需要转码的字符,则这里进行一次编码
_url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
} else {
_url = url;
}
}
- (void)didMoveToSuperview
{
if (!self.superview) {
return;
}
}
- (void)didMoveToWindow
{
if (!self.window) {
if (_tapGesture) {
[self removeGestureRecognizer:self.tapGesture];
}
return;
}
if (!self.isLoaded) {
[self addSubview:self.webViewController.view];
self.isLoaded = YES;
if (self.url.length > 0) {
[self.webViewController loadWebUrl:self.url];
}
}
if (self.enablePictureTapGesture) {
[self addGestureRecognizer:self.tapGesture];
[self.tapGesture requireGestureRecognizerToFail:self.webView.scrollView.panGestureRecognizer];
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (self.isLoaded) {
self.webViewController.view.frame = self.bounds;
}
}
#pragma mark - Public
- (void)loadWebUrl:(NSString *)url
{
self.url = url;
if (self.isLoaded) {
[self.webViewController loadWebUrl:self.url];
}
}
- (void)loadHTMLString:(NSString *)html
{
if (self.isLoaded) {
[self.webViewController loadHTMLString:html];
}
}
- (void)loadHTMLString:(NSString *)html baseURL:(nullable NSURL *)baseURL
{
if (self.isLoaded) {
[self.webViewController loadHTMLString:html baseURL:baseURL];
}
}
- (void)setProgressBarColor:(UIColor *)color
{
[self.webViewController setProgressBarColor:color];
}
#pragma mark - Override Setter & Getter
- (YHExplorerViewController *)webViewController
{
if (!_webViewController) {
_webViewController = [[YHExplorerViewController alloc] initWithCurrentNaviBar:self.currentNaviBar];
}
return _webViewController;
}
- (UIWebView *)webView
{
return self.webViewController.webView;
}
- (UITapGestureRecognizer *)tapGesture {
if (!_tapGesture) {
_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hanldeTapGesture:)];
_tapGesture.delegate = self;
}
return _tapGesture;
}
#pragma mark - Setters
- (void)setDelegate:(id<YHExplorerDelegate>)delegate
{
self.webViewController.delegate = delegate;
}
- (id<YHExplorerDelegate>)delegate
{
return self.webViewController.delegate;
}
- (void)setEnableProgressBar:(BOOL)enableProgressBar
{
self.webViewController.currentNaviBar = self.currentNaviBar;
self.webViewController.progressBarEnabled = enableProgressBar;
}
- (void)setEnablePictureTapGesture:(BOOL)enablePictureTapGesture {
_enablePictureTapGesture = enablePictureTapGesture;
if (!self.window) {
return;
}
if (enablePictureTapGesture) {
if (self.superview) {
[self addGestureRecognizer:self.tapGesture];
[self.tapGesture requireGestureRecognizerToFail:self.webView.scrollView.panGestureRecognizer];
}
} else {
if (_tapGesture) {
[self removeGestureRecognizer:self.tapGesture];
self.tapGesture = nil;
}
}
}
- (BOOL)enableProgressBar
{
return self.webViewController.progressBarEnabled;
}
#pragma mark - Private
- (NSString *)_getImageScriptStringWithPoint:(CGPoint)p {
NSString *string = [NSString stringWithFormat:@"var imageElement = document.elementFromPoint(%f, %f);\
var rect = imageElement.getBoundingClientRect();\
var frame = {x:rect.left, y:rect.top, width:rect.width, height:rect.height};\
var className = imageElement.className;\
var dataSet = { url:imageElement.src, displayFrame:frame, class:className, tagName:imageElement.tagName, id:imageElement.id };\
JSON.stringify(dataSet);", p.x, p.y];
return string;
}
- (NSString *)_getJsStringToValidateGestureAreaById {
if (self.constraintIdsForPictureTapGesture.count == 0) {
return nil;
}
NSMutableString *jsString = [[NSMutableString alloc] initWithString:@"var frameArray = [];"];
for (NSUInteger i=0; i<self.constraintIdsForPictureTapGesture.count; i++) {
[jsString appendFormat:@"var ele%zd = document.getElementById('%@');var rect%zd = ele%zd.getBoundingClientRect();var frame%zd = {x:rect%zd.left, y:rect%zd.top, width:rect%zd.width, height:rect%zd.height};frameArray.push(frame%zd);", i, self.constraintIdsForPictureTapGesture[i], i, i, i, i, i, i, i, i];
}
[jsString appendString:@"JSON.stringify(frameArray);"];
return jsString;
}
- (NSString *)_getJsStringToValidateGestureAreaByClass {
if (self.constraintClassesForPictureTapGesture.count == 0) {
return nil;
}
NSMutableString *jsString = [[NSMutableString alloc] initWithString:@"var frameArray = [];"];
for (NSUInteger i=0; i<self.constraintClassesForPictureTapGesture.count; i++) {
[jsString appendFormat:@"var ele%zd = document.getElementsByClassName('%@');for (var i=0;i<ele%zd.length;i++) {var ele = ele%zd[i]; var rect = ele.getBoundingClientRect();var frame = {x:rect.left, y:rect.top, width:rect.width, height:rect.height};frameArray.push(frame);}", i, self.constraintClassesForPictureTapGesture[i], i, i];
}
[jsString appendString:@"JSON.stringify(frameArray);"];
return jsString;
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if (gestureRecognizer == _tapGesture) {
return YES;
} else {
return NO;
}
}
#pragma mark - Gesture Handlers
- (void)hanldeTapGesture:(UITapGestureRecognizer *)gesture {
if (gesture == _tapGesture) {
CGFloat displayWidth = [[self.webView stringByEvaluatingJavaScriptFromString:@"window.innerWidth"] floatValue];
CGFloat scale = self.webView.frame.size.width / displayWidth;
CGPoint touchPoint = [gesture locationInView:self.webView];
// 如果webview是zoom状态,把对应的比例除去
touchPoint.x /= scale;
touchPoint.y /= scale;
BOOL validArea = YES;
BOOL validIdArea = YES;
BOOL validClassArea = YES;
if (self.constraintIdsForPictureTapGesture.count) {
validIdArea = NO;
NSString *frameArrayString = [self.webView stringByEvaluatingJavaScriptFromString:[self _getJsStringToValidateGestureAreaById]];
if (frameArrayString.length) {
NSData *resultData = [frameArrayString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:resultData
options:0
error:nil];
if (jsonArray.count == self.constraintIdsForPictureTapGesture.count) {
for (NSDictionary *dic in jsonArray) {
NSNumber *rectX = dic[@"x"];
NSNumber *rectY = dic[@"y"];
NSNumber *rectWidth = dic[@"width"];
NSNumber *rectHeight = dic[@"height"];
CGRect rect = CGRectMake(rectX?rectX.doubleValue:0, rectY?rectY.doubleValue:0, rectWidth?rectWidth.doubleValue:0, rectHeight?rectHeight.doubleValue:0);
if (CGRectContainsPoint(rect, touchPoint)) {
validIdArea = YES;
break;
}
}
}
}
}
if (self.constraintClassesForPictureTapGesture.count) {
validClassArea = NO;
NSString *frameArrayString = [self.webView stringByEvaluatingJavaScriptFromString:[self _getJsStringToValidateGestureAreaByClass]];
if (frameArrayString.length) {
NSData *resultData = [frameArrayString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:resultData
options:0
error:nil];
for (NSDictionary *dic in jsonArray) {
NSNumber *rectX = dic[@"x"];
NSNumber *rectY = dic[@"y"];
NSNumber *rectWidth = dic[@"width"];
NSNumber *rectHeight = dic[@"height"];
CGRect rect = CGRectMake(rectX?rectX.doubleValue:0, rectY?rectY.doubleValue:0, rectWidth?rectWidth.doubleValue:0, rectHeight?rectHeight.doubleValue:0);
if (CGRectContainsPoint(rect, touchPoint)) {
validClassArea = YES;
break;
}
}
}
}
if (self.constraintIdsForPictureTapGesture.count > 0 && self.constraintClassesForPictureTapGesture.count > 0) {
validArea = (validIdArea || validClassArea);
} else if (self.constraintIdsForPictureTapGesture.count) {
validArea = validIdArea;
} else if (self.constraintClassesForPictureTapGesture.count) {
validArea = validClassArea;
}
if (!validArea) {
return;
}
NSString *imgURL = [self _getImageScriptStringWithPoint:touchPoint];
NSString *resultString = [self.webView stringByEvaluatingJavaScriptFromString:imgURL];
if (resultString.length) {
NSData *resultData = [resultString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:resultData
options:0
error:nil];
if (json) {
NSString *tagName = [json[@"tagName"] lowercaseString];
if ([tagName isEqualToString:@"img"] && [self.delegate respondsToSelector:@selector(explorer:didClickPicture:)]) {
[self.delegate explorer:self.webViewController didClickPicture:json];
}
}
}
}
}
#pragma mark - Method Forwarding
- (BOOL)respondsToSelector:(SEL)aSelector
{
if ([super respondsToSelector:aSelector]) {
return YES;
}
if ([self.webViewController respondsToSelector:aSelector]) {
return YES;
}
return NO;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
NSMethodSignature *signature = [super methodSignatureForSelector:aSelector];
if (!signature) {
if (self.webViewController && [self.webViewController respondsToSelector:aSelector]) {
return [(NSObject *)self.webViewController methodSignatureForSelector:aSelector];
}
}
return signature;
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
if (self.webViewController && [self.webViewController respondsToSelector:[anInvocation selector]]) {
[anInvocation invokeWithTarget:self.webViewController];
}
}
//移除滚动条
- (void)removeProgress
{
[self.webViewController removeProgress];
}
@end