美文网首页
ios DIY加载进度条

ios DIY加载进度条

作者: Arthur澪 | 来源:发表于2018-02-06 10:38 被阅读0次

    自定义控件——网页的加载进度条

    新建一个类,继承自UIView,在.h中

    #import <UIKit/UIKit.h>
    
    @interface PDloadProcessLine : UIView
    
    //进度条颜色
    @property (nonatomic,strong) UIColor  *lineColor;
    
    //开始加载
    -(void)startLoadingAnimation:(float)y;
    
    //结束加载
    -(void)endLoadingAnimation:(float)y;
    
    @end
    

    在.m文件中
    提供设置进度条颜色的方法

    -(void)setLineColor:(UIColor *)lineColor{
        _lineColor = lineColor;
        self.backgroundColor = lineColor;
    }
    

    创建方法

    -(instancetype)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if (self) {
            self.hidden = YES;
            self.backgroundColor = [UIColor whiteColor];
        }
        return self;
    }
    

    实现开始加载时,进度条动画方法

    -(void)startLoadingAnimation:(float)y{
        self.hidden = NO;
        self.frame = CGRectMake(0, y, 0, 3);
        
        __weak UIView *weakSelf = self;
        
        [UIView animateWithDuration:0.4 animations:^{
    
            weakSelf.frame = CGRectMake(0, y, screenW * 0.4, 3);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.4 animations:^{
    
                weakSelf.frame = CGRectMake(0, y, screenW * 0.8, 3);
            }];
        }];
    }
    

    结束加载时,进度条动画

    -(void)endLoadingAnimation:(float)y{
        __weak UIView *weakSelf = self;
        [UIView animateWithDuration:0.2 animations:^{
    
            weakSelf.frame = CGRectMake(0, y, screenW, 3);
    
        } completion:^(BOOL finished) {
            weakSelf.hidden = YES;
        }];
    }
    

    相关开源库

    https://github.com/ninjinkun/NJKWebViewProgress *
    https://github.com/gsdios/SDProgressView *

    相关文章

      网友评论

          本文标题:ios DIY加载进度条

          本文链接:https://www.haomeiwen.com/subject/rfwtextx.html