WebView进度条

import <Foundation/Foundation.h>

@interface NSTimer (addition)

  • (void)pause;
  • (void)resume;
  • (void)resumeWithTimeInterval:(NSTimeInterval)time;

@end

import "NSTimer+addition.h"

@implementation NSTimer (addition)

  • (void)pause {
    if (!self.isValid) return;
    [self setFireDate:[NSDate distantFuture]];
    }

  • (void)resume {
    if (!self.isValid) return;
    [self setFireDate:[NSDate date]];
    }

  • (void)resumeWithTimeInterval:(NSTimeInterval)time {
    if (!self.isValid) return;
    [self setFireDate:[NSDate dateWithTimeIntervalSinceNow:time]];
    }

@end


@interface RGWebProgressLayer : CAShapeLayer

  • (void)finishedLoad;

  • (void)startLoad;

  • (void)closeTimer;

@end

import "RGWebProgressLayer.h"

import "NSTimer+addition.h"

static NSTimeInterval const kFastTimeInterval = 0.003;

@implementation RGWebProgressLayer{
CAShapeLayer *_layer;

NSTimer *_timer;
CGFloat _plusWidth; ///< 增加点

}

  • (instancetype)init {
    if (self = [super init]) {
    [self initialize];
    }
    return self;
    }
  • (void)initialize {
    self.lineWidth = 2;
    self.strokeColor = [UIColor greenColor].CGColor;

    _timer = [NSTimer scheduledTimerWithTimeInterval:kFastTimeInterval target:self selector:@selector(pathChanged:) userInfo:nil repeats:YES];
    [_timer pause];

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 2)];
    [path addLineToPoint:CGPointMake(KScreen_Width, 2)];

    self.path = path.CGPath;
    self.strokeEnd = 0;
    _plusWidth = 0.01;
    }

  • (void)pathChanged:(NSTimer *)timer {
    self.strokeEnd += _plusWidth;

    if (self.strokeEnd > 0.8) {
    _plusWidth = 0.002;
    }
    }

  • (void)startLoad {
    [_timer resumeWithTimeInterval:kFastTimeInterval];
    }

  • (void)finishedLoad {
    [self closeTimer];

    self.strokeEnd = 1.0;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    self.hidden = YES;
    [self removeFromSuperlayer];
    });
    }

  • (void)dealloc {
    // NSLog(@"progressView dealloc");
    [self closeTimer];
    }

pragma mark - private

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

推荐阅读更多精彩内容