// // WLWebProgressLayer.m // WangliBank // // Created by 王启镰 on 16/6/22. // Copyright © 2016年 iSoftstone infomation Technology (Group) Co.,Ltd. All rights reserved. // #import "WLWebProgressLayer.h" #import "NSTimer+addition.h" #import static NSTimeInterval const kFastTimeInterval = 0.03; #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width @implementation WLWebProgressLayer { CAShapeLayer *_layer; NSTimer *_timer; CGFloat _plusWidth; ///< 增加点 } + (instancetype)layerWithFrame:(CGRect)frame { WLWebProgressLayer *layer = [self new]; layer.frame = frame; return layer; } - (instancetype)init { if (self = [super init]) { [self initialize]; } return self; } - (void)initialize { self.anchorPoint = CGPointMake(0, 0.5); self.lineWidth = 2; self.strokeColor = [UIColor orangeColor].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(SCREEN_WIDTH, 2)]; self.path = path.CGPath; self.strokeEnd = 0; _plusWidth = 0.01; } - (void)pathChanged:(NSTimer *)timer { if (self.strokeEnd >= 0.97) { [_timer pause]; return; } [CATransaction begin]; [CATransaction setDisableActions:YES]; self.strokeEnd += _plusWidth; if (self.strokeEnd > 0.8) { _plusWidth = 0.001; } [CATransaction commit]; } - (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(@"%s", __func__); [self closeTimer]; } #pragma mark - private - (void)closeTimer { [_timer invalidate]; _timer = nil; } @end