WKWebView增多进程条,WKWebview上加载进程条

WKWebView进度及title

WKWebView进度及title

WKWebView 的estimatedProgress和title
都以KVO格局,所以能够加上监察和控制:

    [webView addObserver:self forKeyPath:@”estimatedProgress” options:NSKeyValueObservingOptionNew context:NULL];

    [webView addObserver:self forKeyPath:@”title”
options:NSKeyValueObservingOptionNew context:NULL];

  •  

监督的贯彻格局:

  • (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void
    *)context
    {

 

    if ([keyPath
isEqualToString:@”estimatedProgress”]) {

 

        if (object ==
webView) {

            [self.progressView setAlpha:1.0f];

            [self.progressView setProgress:self.currentSubView.webView.estimatedProgress
animated:YES];

 

            if(self.currentSubView.webView.estimatedProgress
>= 1.0f)
{

 

                [UIView animateWithDuration:0.3
delay:0.3
options:UIViewAnimationOptionCurveEaseOut
animations:^{

                    [self.progressView setAlpha:0.0f];

                } completion:^(BOOL finished) {

                    [self.progressView setProgress:0.0f animated:NO];

                }];

 

            }

        }

        else

        {

            [super
observeValueForKeyPath:keyPath ofObject:object change:change
context:context];

WKWebView增多进程条,WKWebview上加载进程条。        }

 

    }

    else if ([keyPath isEqualToString:@”title”])

    {

        if (object ==
self.webView)
{

            self.title = self.webView.title;

 

        }

        else

        {

            [super
observeValueForKeyPath:keyPath ofObject:object change:change
context:context];

 

        }

    }

    else
{

 

        [super
observeValueForKeyPath:keyPath ofObject:object change:change
context:context];

    }

}

  •  

此地的速度扩充了动画,类似safari的速度效果

亟需注意的是绝迹的时候自然要移除监察和控制

        [webView removeObserver:self forKeyPath:@”estimatedProgress”];

        [webView removeObserver:self forKeyPath:@”title”];

 

swift:

// 监听

        theWebView?.addObserver(self, forKeyPath: “estimatedProgress”, options: NSKeyValueObservingOptions.New, context:
nil)

        theWebView?.addObserver(self, forKeyPath: “title”, options: NSKeyValueObservingOptions.New, context:
nil)

 

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>)
{

        if keyPath ==
“estimatedProgress”
{

 

            if
((object?.isEqual(theWebView)) !=
false)
{

                 self.progressView.alpha = 1.0

                self.progressView.setProgress(Float((self.theWebView?.estimatedProgress)!), animated: true)

 

                if
self.theWebView?.estimatedProgress >=
1.0
{

 

                    UIView.animateWithDuration(0.3, delay: 0.3, options: .CurveEaseOut, animations:
{

                        self.progressView.alpha = 0.0

                        }, completion: { (finished)
in

                            self.progressView.setProgress(0.0, animated: false)

                    })

                }

            }else
{

                super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context:
context)

            }

        }

    }

    deinit
{

        webView.removeObserver(self, forKeyPath: “estimatedProgress”)

        webView.removeObserver(self, forKeyPath: “title”)

    }

WKWebView进度及title

WKWebView进度及title

WKWebView 的estimatedProgress和title
都以KVO形式,所以能够加上监察和控制:

    [webView addObserver:self forKeyPath:@”estimatedProgress” options:NSKeyValueObservingOptionNew context:NULL];

    [webView addObserver:self forKeyPath:@”title”
options:NSKeyValueObservingOptionNew context:NULL];

  •  

监理的落到实处格局:

  • (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void
    *)context
    {

 

    if ([keyPath
isEqualToString:@”estimatedProgress”]) {

 

        if (object ==
webView) {

            [self.progressView setAlpha:1.0f];

            [self.progressView setProgress:self.currentSubView.webView.estimatedProgress
animated:YES];

 

            if(self.currentSubView.webView.estimatedProgress
>= 1.0f)
{

 

                [UIView animateWithDuration:0.3
delay:0.3
options:UIViewAnimationOptionCurveEaseOut
animations:^{

                    [self.progressView setAlpha:0.0f]澳门葡京备用网址,;

                } completion:^(BOOL finished) {

                    [self.progressView setProgress:0.0f animated:NO];

                }];

 

            }

        }

        else

        {

            [super
observeValueForKeyPath:keyPath ofObject:object change:change
context:context];

        }

 

    }

    else if ([keyPath isEqualToString:@”title”])

    {

        if (object ==
self.webView)
{

            self.title = self.webView.title;

 

        }

        else

        {

            [super
observeValueForKeyPath:keyPath ofObject:object change:change
context:context];

 

        }

    }

    else
{

 

        [super
observeValueForKeyPath:keyPath ofObject:object change:change
context:context];

    }

}

  •  

那里的速度扩大了动画片,类似safari的快慢效果

亟待留意的是绝迹的时候势供给移除监察和控制

        [webView removeObserver:self forKeyPath:@”estimatedProgress”];

        [webView removeObserver:self forKeyPath:@”title”];

 

swift:

// 监听

        theWebView?.addObserver(self, forKeyPath: “estimatedProgress”, options: NSKeyValueObservingOptions.New, context:
nil)

        theWebView?.addObserver(self, forKeyPath: “title”, options: NSKeyValueObservingOptions.New, context:
nil)

 

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>)
{

        if keyPath ==
“estimatedProgress”
{

 

            if
((object?.isEqual(theWebView)) !=
false)
{

                 self.progressView.alpha = 1.0

                self.progressView.setProgress(Float((self.theWebView?.estimatedProgress)!), animated: true)

 

                if
self.theWebView?.estimatedProgress >=
1.0
{

 

                    UIView.animateWithDuration(0.3, delay: 0.3, options: .CurveEaseOut, animations:
{

                        self.progressView.alpha = 0.0

                        }, completion: { (finished)
in

                            self.progressView.setProgress(0.0, animated: false)

                    })

                }

            }else
{

                super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context:
context)

            }

        }

    }

    deinit
{

        webView.removeObserver(self, forKeyPath: “estimatedProgress”)

        webView.removeObserver(self, forKeyPath: “title”)

    }

一.导入头文件

下边介绍在webview中增多加载进度条的代码。具体的思绪看代码一目明白

#import <WebKit/WebKit.h>

[self.webView addObserver:self forKeyPath:@”estimatedProgress”
options:NSKeyValueObservingOptionNew context:nil];

– (void)webView:(WKWebView *)webView
didStartProvisionalNavigation:(null_unspecified WKNavigation
*)navigation {  

  DLOG(@”%@”, webView.URL);   

  [self.progressView setProgress:.15 animated:YES];

}

// 计算wkWebView进度条

– (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object change:(NSDictionary *)change context:(void
*)context {  

  if (object == self.newedWebView && [keyPath
isEqualToString:@”estimatedProgress”]) {        

CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey]
doubleValue];   

  if (self.progressView.hidden) self.progressView.hidden = NO;    

    [self.progressView setProgress:newprogress animated:YES];    

    if (newprogress == 1) {               

          dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{  
     

        self.progressView.hidden = YES;      

      });         }    

}else{    

    [super observeValueForKeyPath:keyPath ofObject:object
change:change context:context];  

  }

}

– (UIProgressView *)progressView {  

  if(!_progressView)     {  

      _progressView = [[UIProgressView alloc]
initWithFrame:CGRectMake(0, self.navBar.maxY, MSWIDTH, 0)];   

    _progressView.progressViewStyle = UIProgressViewStyleBar;      

  _progressView.tintColor = RGB(218, 187, 63);    

    _progressView.trackTintColor = [UIColor clearColor];      

  [self.view addSubview:_progressView];     }    

return _progressView;

}

– (void)dealloc {

   [self.newedWebView removeObserver:self
forKeyPath:@”estimatedProgress”];

}

二.创办WKWebView和ProcessView,
并且增加观望者,代理WKUIDelegate,WKNavigationDelegate

@property (strong, nonatomic) WKWebView *webView;

@property (strong, nonatomic) UIProgressView *progressView;

– (void)viewDidLoad {

[super viewDidLoad];

self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];

self.webView.UIDelegate = self;

self.webView.navigationDelegate = self;

[self.view addSubview:self.webView];

self.progressView = [[UIProgressView
alloc]initWithFrame:CGRectMake(0, 64,
CGRectGetWidth(self.view.frame),2)];

[self.view addSubview:self.progressView];

[self.webView addObserver:self forKeyPath:@”estimatedProgress”
options:NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld
context:nil];

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL
URLWithString:@”];

}

  1. 达成代理方法

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary*)change context:(void *)context

{

if ([keyPath isEqual: @”estimatedProgress”] && object == self.webView)
{

[self.progressView setAlpha:1.0f];

[self.progressView setProgress:self.webView.estimatedProgress
animated:YES];

if(self.webView.estimatedProgress >= 1.0f)

{

[UIView animateWithDuration:0.3 delay:0.3
options:UIViewAnimationOptionCurveEaseOut animations:^{

[self.progressView setAlpha:0.0f];

} completion:^(BOOL finished) {

[self.progressView setProgress:0.0f animated:NO];

}];

}

}

else {

[super observeValueForKeyPath:keyPath ofObject:object change:change
context:context];

}

}

四.移除观望者

– (void)dealloc {

[self.webView removeObserver:self forKeyPath:@”estimatedProgress”];

[self.webView setNavigationDelegate:nil];

[self.webView setUIDelegate:nil];

}

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website