iOS WKWebView添加進(jìn)度條

給WKWebView添加進(jìn)度條的簡單展示

Q1: 以什么方式來添加?

A: 當(dāng)然是“分類”的方式~(為什么?因為這樣代碼維護(hù)容易、耦合度低,“只聊情感,不走進(jìn)生活”)

Q2: 分類中不是不能添加屬性嗎?

A: 是的,直接添加使用不可以,但是配合一些運行時的手腳就可以了。

要做的主要內(nèi)容是什么?

    1. 設(shè)計一個進(jìn)度條、有默認(rèn)可通用
      設(shè)計一個通用的進(jìn)度條就要給它限定在我們的可控范圍內(nèi),這里我們可以編寫一個協(xié)議讓使用者去遵守:
@protocol RCProgressDelegate <NSObject>

@required

///設(shè)置進(jìn)度
- (void)rc_setProgress:(CGFloat)progress animated:(BOOL)animated;

///設(shè)置進(jìn)度條顏色(設(shè)置顏色一般就可以了,獲取顏色必要性不是很大)
- (void)rc_setProgressColor:(UIColor *)color;

///設(shè)置進(jìn)度條背景顏色
- (void)rc_setTrackColor:(UIColor *)color;

@optional

///父視圖可能的偏移,比如scrollView相對UINavigationBar的偏移, e.g. automaticallyAdjustsScrollViewInsets, contentInsetAdjustmentBehavior etc.
- (CGPoint)rc_contentOffset;

@end

類中添加進(jìn)度協(xié)議的視圖屬性

@interface WKWebView (RCProgressView)

///自定義進(jìn)度條視圖要滿足‘RCProgressDelegate’,默認(rèn)提供進(jìn)度條視圖
@property (nonatomic, strong) UIView<RCProgressDelegate> *progressView;

@end
    1. 添加屬性方便使用
      分類中直接使用 @property 去定義屬性是不能直接使用的,這么做只是聲明setter/getter并沒有實現(xiàn)它并且也不會自動合成"_xxx"變量。所以需要編程人員自己去實現(xiàn)setter/getter方法。另外有警告的話還需要“@dynamic xx;”來告訴編譯器setter/getter由我來實現(xiàn)你就不需要操心了。
      涉及函數(shù):
#import <objc/runtime.h>

//設(shè)置關(guān)聯(lián)屬性值
void objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key,
                         id _Nullable value, objc_AssociationPolicy policy);

解釋:
object: 屬性的擁有者
key: 唯一鍵值,一般使用 static聲明個字符串指針使用該地址值即可,如:static NSString *key = @"xxx",使用'&key'就可以了.
value: 設(shè)置的關(guān)聯(lián)值
policy: 下面5個可選值
OBJC_ASSOCIATION_ASSIGN 相當(dāng)于 @property(weak, atomic)
OBJC_ASSOCIATION_RETAIN_NONATOMIC 相當(dāng)于 @property(strong, nonatomic)
OBJC_ASSOCIATION_COPY_NONATOMIC 相當(dāng)于 @property(copy, nonatomic)
OBJC_ASSOCIATION_RETAIN 相當(dāng)于 @property(strong, atomic)
OBJC_ASSOCIATION_COPY 相當(dāng)于 @property(copy, atomic)

//獲取關(guān)聯(lián)屬性值
id _Nullable objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key);

解釋:
object: 關(guān)聯(lián)者,屬性的擁有者
key: 關(guān)聯(lián)值所在的‘地址’

要怎么用?

有默認(rèn)的進(jìn)度條,如果需要自定義進(jìn)度條的話遵守RCProgressDelegate協(xié)議即可。
舉個例子,簡單自定義進(jìn)度條:

///定義
@interface RCProgressView : UIView <RCProgressDelegate>
@end

///實現(xiàn)
@interface RCProgressView()

@property (nonatomic, weak) UIView *progress;

@end

@implementation RCProgressView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if(self = [super initWithFrame:frame]){
        
            UIView *view = [[UIView alloc] initWithFrame:self.bounds];
            view.backgroundColor = UIColor.redColor;
             [self addSubview:view];

            view.layer.anchorPoint = CGPointMake(0, 0.5);
            _progress = view;
            
            view.frame = self.bounds;
    }
    
    return self;
}

- (void)rc_setProgress:(CGFloat)progress animated:(BOOL)animated {
    
    __typeof(&*self) __weak wself = self;
    [UIView animateWithDuration:animated ? .2f : 0.f animations:^{
        
        wself.progress.transform = CGAffineTransformMakeScale(progress, 1);
    }];
}

- (void)rc_setProgressColor:(UIColor *)color {
    
    self.progress.backgroundColor = color;
}

- (void)rc_setTrackColor:(UIColor *)color {
    
    self.backgroundColor = color;
}

@end

項目地址

項目地址,后續(xù)我再補上

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容