封装一个WKWebView(带进度条)

0C25136F-76CF-4F1A-91BB-6466CE5D45D3.png

二话不说,先丢效果图😄😄。

最近用hud感觉界面不美观,而且加载页面的视觉速度觉得特别慢,就写了个基于WKWebView的进度条,分享给大家,不多说,直接上干货

#import <UIKit/UIKit.h>

@interface WWJ_WKWebView : UIView
/**
 *  进度条的颜色
 */
@property (strong, nonatomic) UIColor *progressColor;

- (instancetype)initWithUrl:(NSURL *)url;

@end
#import "WWJ_WKWebView.h"
#import <WebKit/WebKit.h>
#import <objc/runtime.h>
NSString *const progressColorKey = @"progressColorKey";
@interface WWJ_WKWebView ()

@property (strong, nonatomic) CALayer *progresslayer;

@end

@implementation WWJ_WKWebView

- (void)initWithDefault{
    self.progressColor = [UIColor colorWithRed:22.f / 255.f green:126.f / 255.f blue:251.f / 255.f alpha:.8];;
}

- (void)initWithProgressView{
    UIView *progress = [[UIView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.frame), 3)];
    progress.backgroundColor = [UIColor clearColor];
    [self addSubview:progress];
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, 0, 0, 3);
    layer.backgroundColor = self.progressColor.CGColor;
    [progress.layer addSublayer:layer];
    self.progresslayer = layer;
}

- (UIColor *)progressColor{
    return objc_getAssociatedObject(self, &progressColorKey);
}

- (void)setProgressColor:(UIColor *)progressColor{
    objc_setAssociatedObject(self, &progressColorKey, progressColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self initWithProgressView];
}

- (instancetype)initWithUrl:(NSURL *)url{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        WKWebView *webView = [[WKWebView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        //添加观察者,观察wkwebview的estimatedProgress属性
        [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
        [self addSubview:webView];
        [webView loadRequest:[NSURLRequest requestWithURL:url]];
        [self initWithDefault];
        [self initWithProgressView];
    }
    return self;
}

- (void)dealloc{
    [(WKWebView *)self removeObserver:self forKeyPath:@"estimatedProgress"];
}

- (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.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];
    }
}

@end

之后调用就十分简单了

WWJ_WKWebView *webView = [[WWJ_WKWebView alloc] initWithUrl:[NSURL URLWithString:@"http://shaozi.info"]];
    webView.progressColor = [UIColor orangeColor];
    [self.view addSubview:webView];

最后丢一张效果的gif

progress.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,972评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,267评论 4 61
  • “我这一生会遇见许多姑娘,无论以前或者以后,都会随时遇见,但还能让我死心塌地爱上的,估计没有了,除了刘晓娇,不可能...
    月零星阅读 811评论 0 1
  • 为何你与别人的差距如此之大,因为你在看,他早已完成,这就是效率带来的影响。 今天自由写作空间的Rita邀请鸡血学院...
    海豚的世界阅读 540评论 0 0
  • 亲爱的朋友们大家好!我是网络骄阳,今天给大家带来一款《鸡蛋饼》,这是一款非常不错的早餐,这款早餐一定会受到家人的欢...
    feaf5a1303da阅读 542评论 14 36