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;
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容