forked from iStig/JavaScriptCoreDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRxWebViewController.m
More file actions
executable file
·425 lines (354 loc) · 14.3 KB
/
Copy pathRxWebViewController.m
File metadata and controls
executable file
·425 lines (354 loc) · 14.3 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//
// RxWebViewController.m
// RxWebViewController
//
// Created by roxasora on 15/10/23.
// Copyright © 2015年 roxasora. All rights reserved.
//
#import "RxWebViewController.h"
#import "NJKWebViewProgress.h"
#import "NJKWebViewProgressView.h"
#define boundsWidth self.view.bounds.size.width
#define boundsHeight self.view.bounds.size.height
@interface RxWebViewController ()<UIWebViewDelegate,UINavigationControllerDelegate,UINavigationBarDelegate,NJKWebViewProgressDelegate>
@property (nonatomic)UIBarButtonItem* customBackBarItem;
@property (nonatomic)UIBarButtonItem* closeButtonItem;
@property (nonatomic)NJKWebViewProgress* progressProxy;
@property (nonatomic)NJKWebViewProgressView* progressView;
/**
* array that hold snapshots
*/
@property (nonatomic)NSMutableArray* snapShotsArray;
/**
* current snapshotview displaying on screen when start swiping
*/
@property (nonatomic)UIView* currentSnapShotView;
/**
* previous view
*/
@property (nonatomic)UIView* prevSnapShotView;
/**
* background alpha black view
*/
@property (nonatomic)UIView* swipingBackgoundView;
/**
* left pan ges
*/
@property (nonatomic)UIPanGestureRecognizer* swipePanGesture;
/**
* if is swiping now
*/
@property (nonatomic)BOOL isSwipingBack;
@end
@implementation RxWebViewController
-(UIStatusBarStyle) preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
#pragma mark - init
-(instancetype)initWithUrl:(NSURL *)url{
self = [super init];
if (self) {
self.url = url;
_progressViewColor = [UIColor colorWithRed:119.0/255 green:228.0/255 blue:115.0/255 alpha:1];
}
return self;
}
- (void)viewDidLoad{
[super viewDidLoad];
self.title = @"";
self.view.backgroundColor = [UIColor whiteColor];
//config navigation item
self.navigationItem.leftItemsSupplementBackButton = YES;
self.webView.delegate = self.progressProxy;
[self.view addSubview:self.webView];
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
[self.navigationController.navigationBar addSubview:self.progressView];
// Do any additional setup after loading the view.
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[self.progressView removeFromSuperview];
self.webView.delegate = nil;
}
#pragma mark - public funcs
-(void)reloadWebView{
[self.webView reload];
}
#pragma mark - logic of push and pop snap shot views
-(void)pushCurrentSnapshotViewWithRequest:(NSURLRequest*)request{
// NSLog(@"push with request %@",request);
NSURLRequest* lastRequest = (NSURLRequest*)[[self.snapShotsArray lastObject] objectForKey:@"request"];
//如果url是很奇怪的就不push
if ([request.URL.absoluteString isEqualToString:@"about:blank"]) {
// NSLog(@"about blank!! return");
return;
}
//如果url一样就不进行push
if ([lastRequest.URL.absoluteString isEqualToString:request.URL.absoluteString]) {
return;
}
UIView* currentSnapShotView = [self.webView snapshotViewAfterScreenUpdates:YES];
[self.snapShotsArray addObject:
@{
@"request":request,
@"snapShotView":currentSnapShotView
}
];
// NSLog(@"now array count %d",self.snapShotsArray.count);
}
-(void)startPopSnapshotView{
if (self.isSwipingBack) {
return;
}
if (!self.webView.canGoBack) {
return;
}
self.isSwipingBack = YES;
//create a center of scrren
CGPoint center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
self.currentSnapShotView = [self.webView snapshotViewAfterScreenUpdates:YES];
//add shadows just like UINavigationController
self.currentSnapShotView.layer.shadowColor = [UIColor blackColor].CGColor;
self.currentSnapShotView.layer.shadowOffset = CGSizeMake(3, 3);
self.currentSnapShotView.layer.shadowRadius = 5;
self.currentSnapShotView.layer.shadowOpacity = 0.75;
//move to center of screen
self.currentSnapShotView.center = center;
self.prevSnapShotView = (UIView*)[[self.snapShotsArray lastObject] objectForKey:@"snapShotView"];
center.x -= 60;
self.prevSnapShotView.center = center;
self.prevSnapShotView.alpha = 1;
self.view.backgroundColor = [UIColor blackColor];
[self.view addSubview:self.prevSnapShotView];
[self.view addSubview:self.swipingBackgoundView];
[self.view addSubview:self.currentSnapShotView];
}
-(void)popSnapShotViewWithPanGestureDistance:(CGFloat)distance{
if (!self.isSwipingBack) {
return;
}
if (distance <= 0) {
return;
}
CGPoint currentSnapshotViewCenter = CGPointMake(boundsWidth/2, boundsHeight/2);
currentSnapshotViewCenter.x += distance;
CGPoint prevSnapshotViewCenter = CGPointMake(boundsWidth/2, boundsHeight/2);
prevSnapshotViewCenter.x -= (boundsWidth - distance)*60/boundsWidth;
// NSLog(@"prev center x%f",prevSnapshotViewCenter.x);
self.currentSnapShotView.center = currentSnapshotViewCenter;
self.prevSnapShotView.center = prevSnapshotViewCenter;
self.swipingBackgoundView.alpha = (boundsWidth - distance)/boundsWidth;
}
-(void)endPopSnapShotView{
if (!self.isSwipingBack) {
return;
}
//prevent the user touch for now
self.view.userInteractionEnabled = NO;
if (self.currentSnapShotView.center.x >= boundsWidth) {
// pop success
[UIView animateWithDuration:0.2 animations:^{
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.currentSnapShotView.center = CGPointMake(boundsWidth*3/2, boundsHeight/2);
self.prevSnapShotView.center = CGPointMake(boundsWidth/2, boundsHeight/2);
self.swipingBackgoundView.alpha = 0;
}completion:^(BOOL finished) {
[self.prevSnapShotView removeFromSuperview];
[self.swipingBackgoundView removeFromSuperview];
[self.currentSnapShotView removeFromSuperview];
[self.webView goBack];
[self.snapShotsArray removeLastObject];
self.view.userInteractionEnabled = YES;
self.isSwipingBack = NO;
}];
}else{
//pop fail
[UIView animateWithDuration:0.2 animations:^{
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.currentSnapShotView.center = CGPointMake(boundsWidth/2, boundsHeight/2);
self.prevSnapShotView.center = CGPointMake(boundsWidth/2-60, boundsHeight/2);
self.prevSnapShotView.alpha = 1;
}completion:^(BOOL finished) {
[self.prevSnapShotView removeFromSuperview];
[self.swipingBackgoundView removeFromSuperview];
[self.currentSnapShotView removeFromSuperview];
self.view.userInteractionEnabled = YES;
self.isSwipingBack = NO;
}];
}
}
#pragma mark - update nav items
-(void)updateNavigationItems{
if (self.webView.canGoBack) {
UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spaceButtonItem.width = -6.5;
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
[self.navigationItem setLeftBarButtonItems:@[self.closeButtonItem] animated:NO];
//弃用customBackBarItem,使用原生backButtonItem
// [self.navigationItem setLeftBarButtonItems:@[spaceButtonItem,self.customBackBarItem,self.closeButtonItem] animated:NO];
}else{
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
[self.navigationItem setLeftBarButtonItems:nil];
}
}
#pragma mark - events handler
-(void)swipePanGestureHandler:(UIPanGestureRecognizer*)panGesture{
CGPoint translation = [panGesture translationInView:self.webView];
CGPoint location = [panGesture locationInView:self.webView];
// NSLog(@"pan x %f,pan y %f",translation.x,translation.y);
if (panGesture.state == UIGestureRecognizerStateBegan) {
if (location.x <= 50 && translation.x > 0) { //开始动画
[self startPopSnapshotView];
}
}else if (panGesture.state == UIGestureRecognizerStateCancelled || panGesture.state == UIGestureRecognizerStateEnded){
[self endPopSnapShotView];
}else if (panGesture.state == UIGestureRecognizerStateChanged){
[self popSnapShotViewWithPanGestureDistance:translation.x];
}
}
-(void)customBackItemClicked{
[self.webView goBack];
}
-(void)closeItemClicked{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - webView delegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
// NSLog(@"navigation type %d",navigationType);
switch (navigationType) {
case UIWebViewNavigationTypeLinkClicked: {
[self pushCurrentSnapshotViewWithRequest:request];
break;
}
case UIWebViewNavigationTypeFormSubmitted: {
[self pushCurrentSnapshotViewWithRequest:request];
break;
}
case UIWebViewNavigationTypeBackForward: {
break;
}
case UIWebViewNavigationTypeReload: {
break;
}
case UIWebViewNavigationTypeFormResubmitted: {
break;
}
case UIWebViewNavigationTypeOther: {
[self pushCurrentSnapshotViewWithRequest:request];
break;
}
default: {
break;
}
}
[self updateNavigationItems];
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[self updateNavigationItems];
NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
if (theTitle.length > 10) {
theTitle = [[theTitle substringToIndex:9] stringByAppendingString:@"…"];
}
self.title = theTitle;
// [self.progressView setProgress:1 animated:NO];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
#pragma mark - NJProgress delegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
[self.progressView setProgress:progress animated:NO];
}
#pragma mark - setters and getters
-(void)setUrl:(NSURL *)url{
_url = url;
}
-(void)setProgressViewColor:(UIColor *)progressViewColor{
_progressViewColor = progressViewColor;
self.progressView.progressColor = progressViewColor;
}
-(UIWebView*)webView{
if (!_webView) {
_webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
_webView.delegate = (id)self;
_webView.scalesPageToFit = YES;
_webView.backgroundColor = [UIColor whiteColor];
[_webView addGestureRecognizer:self.swipePanGesture];
}
return _webView;
}
-(UIBarButtonItem*)customBackBarItem{
if (!_customBackBarItem) {
UIImage* backItemImage = [[UIImage imageNamed:@"backItemImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImage* backItemHlImage = [[UIImage imageNamed:@"backItemImage-hl"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIButton* backButton = [[UIButton alloc] init];
[backButton setTitle:@"返回" forState:UIControlStateNormal];
[backButton setTitleColor:self.navigationController.navigationBar.tintColor forState:UIControlStateNormal];
[backButton setTitleColor:[self.navigationController.navigationBar.tintColor colorWithAlphaComponent:0.5] forState:UIControlStateHighlighted];
[backButton.titleLabel setFont:[UIFont systemFontOfSize:17]];
[backButton setImage:backItemImage forState:UIControlStateNormal];
[backButton setImage:backItemHlImage forState:UIControlStateHighlighted];
[backButton sizeToFit];
[backButton addTarget:self action:@selector(customBackItemClicked) forControlEvents:UIControlEventTouchUpInside];
_customBackBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}
return _customBackBarItem;
}
-(UIBarButtonItem*)closeButtonItem{
if (!_closeButtonItem) {
_closeButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(closeItemClicked)];
}
return _closeButtonItem;
}
-(UIView*)swipingBackgoundView{
if (!_swipingBackgoundView) {
_swipingBackgoundView = [[UIView alloc] initWithFrame:self.view.bounds];
_swipingBackgoundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
}
return _swipingBackgoundView;
}
-(NSMutableArray*)snapShotsArray{
if (!_snapShotsArray) {
_snapShotsArray = [NSMutableArray array];
}
return _snapShotsArray;
}
-(BOOL)isSwipingBack{
if (!_isSwipingBack) {
_isSwipingBack = NO;
}
return _isSwipingBack;
}
-(UIPanGestureRecognizer*)swipePanGesture{
if (!_swipePanGesture) {
_swipePanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipePanGestureHandler:)];
}
return _swipePanGesture;
}
-(NJKWebViewProgress*)progressProxy{
if (!_progressProxy) {
_progressProxy = [[NJKWebViewProgress alloc] init];
_progressProxy.webViewProxyDelegate = (id)self;
_progressProxy.progressDelegate = (id)self;
}
return _progressProxy;
}
-(NJKWebViewProgressView*)progressView{
if (!_progressView) {
CGFloat progressBarHeight = 3.0f;
CGRect navigaitonBarBounds = self.navigationController.navigationBar.bounds;
// CGRect barFrame = CGRectMake(0, navigaitonBarBounds.size.height - progressBarHeight-0.5, navigaitonBarBounds.size.width, progressBarHeight);
CGRect barFrame = CGRectMake(0, navigaitonBarBounds.size.height, navigaitonBarBounds.size.width, progressBarHeight);
_progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
_progressView.progressColor = self.progressViewColor;
_progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
}
return _progressView;
}
@end