zoukankan      html  css  js  c++  java
  • iOS WKWebView添加进度条02

    之前写了一个是关于webview添加进度条的,现在补一个WKWebView进度条。
    //添加一个全局属性
    @property(nonatomic,strong)CALayer *progresslayer;
     
    //viewDidLoad里添加监听
    [_webViewaddObserver:selfforKeyPath:@"estimatedProgress"options:NSKeyValueObservingOptionNewcontext:nil];
     
    //进度条
     UIView *progress = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 3)];
        progress.backgroundColor = [UIColor clearColor];
        [self.view addSubview:progress];
       
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(0, 0, 0, 3);
        layer.backgroundColor = [UIColor greenColor].CGColor;
        [progress.layer addSublayer:layer];
        self.progresslayer = layer;
     

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
        if ([keyPath isEqualToString:@"estimatedProgress"]) {
            self.progresslayer.opacity = 1;
            //不要让进度条倒着走...有时候goback会出现这种情况
            if ([change[@"new"] floatValue] < [change[@"old"] floatValue]) {
                return;
            }
            self.progresslayer.frame = CGRectMake(0, 0, self.view.bounds.size.width * [change[@"new"] floatValue], 3);
            if ([change[@"new"] floatValue] == 1) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    self.progresslayer.opacity = 0;
                });
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    self.progresslayer.frame = CGRectMake(0, 0, 0, 3);
                });
            }
        }else{
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }
    //移除监听
    -(void)viewWillDisappear:(BOOL)animated{
        [super viewWillDisappear:animated];
        [_webView removeObserver:self forKeyPath:@"estimatedProgress"];
    }
  • 相关阅读:
    Atitit.auto complete 自动完成控件的实现总结
    Atitit. .net c# web 跟客户端winform 的ui控件结构比较
    Atitit.实现继承的原理and方法java javascript .net c# php ...
    Atitit.javascript 实现类的方式原理大总结
    Atitit. BigConfirmTips 控件 大数据量提示确认控件的原理and总结O9
    Atitit..组件化事件化的编程模型--(2)---------Web datagridview 服务器端控件的实现原理and总结
    c#类库中使用Session
    Oracle和Redhat下载地址
    IIS (HTTP Error 500.21
    浅淡Windows7 32位与64位/x86与x64的区别
  • 原文地址:https://www.cnblogs.com/somethingWithiOS/p/6509590.html
Copyright © 2011-2022 走看看