2019-04-03

import "JDTView.h"

@interface JDTView()
//进度圆环
@property (nonatomic, strong) CAShapeLayer *shapeLayer;

@property (nonatomic, strong)NSTimer *timer;


@end

static CGFloat endFloat;

@implementation JDTView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setBackgroundColor:[UIColor whiteColor]];
    }
    [self initUI];
    return self;
}

- (void)initUI{
//    [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.width / 2, self.height / 2) radius:(self.width - 20)/2 startAngle:M_PI / 4 + M_PI / 2 endAngle:M_PI / 4 clockwise:YES]
    
    UIBezierPath * bezier = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:self.frame.size.width/2 startAngle:0  endAngle:M_PI * 2  clockwise:YES];
    [[UIColor blackColor]set];
    bezier.lineWidth = 2;
    [bezier stroke];
    
    CAShapeLayer *bgLayer = [CAShapeLayer layer];
    bgLayer.frame = self.bounds;
    bgLayer.fillColor = [UIColor clearColor].CGColor;//填充色 -  透明
    bgLayer.lineWidth = 20.f;
    bgLayer.strokeColor = [UIColor groupTableViewBackgroundColor].CGColor;//线条颜色
    bgLayer.strokeStart = 0;//起始点
    bgLayer.strokeEnd = 1;//终点
//    bgLayer.lineCap = kCALineCapRound;//让线两端是圆滑的状态
    bgLayer.path = bezier.CGPath;//这里就是把背景的路径设为之前贝塞尔曲线的那个路径
    [self.layer addSublayer:bgLayer];
    
    
    _shapeLayer = [CAShapeLayer layer];
    _shapeLayer.frame = self.bounds;
    _shapeLayer.fillColor = [UIColor clearColor].CGColor;
    _shapeLayer.lineWidth = 20.0f;
    _shapeLayer.lineCap = kCALineCapRound;
    _shapeLayer.strokeColor = [UIColor blueColor].CGColor;
    _shapeLayer.strokeStart = 0;
    _shapeLayer.strokeEnd = 0;
    _shapeLayer.path = bezier.CGPath;
    [self.layer addSublayer:_shapeLayer];
    
}

- (void)animateToProgress:(CGFloat)progress{
    
//    if(_shapeLayer.strokeEnd != 0){
//        [self animateToZero];
//    }
    
    __weak typeof(self)weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_shapeLayer.strokeEnd * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [weakSelf deleteTimer];
        
        NSString *progressStr = [NSString stringWithFormat:@"%lf",progress];
        NSDictionary *userInfo = @{@"progressStr":progressStr};
        
        weakSelf.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:weakSelf selector:@selector(animate:) userInfo:userInfo repeats:YES];
    });
}

- (void)animate:(NSTimer *)time{
    
    CGFloat progress = [[time.userInfo objectForKey:@"progressStr"]  floatValue];
    
    if(_shapeLayer.strokeEnd <= progress){
        _shapeLayer.strokeEnd += 0.01;
    }else{
        [self deleteTimer];
    }
}


//暂停
- (void)animateToZero{
    //获取暂停时当前的 endFloat
    endFloat = _shapeLayer.strokeEnd;
    
    [self deleteTimer];
    
    NSString *progressStr = [NSString stringWithFormat:@"%lf",endFloat];
    NSDictionary *userInfo = @{@"endStr":progressStr};
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(animateReset:) userInfo:userInfo repeats:YES];
}


- (void)animateReset:(NSTimer *)time{
    if(_shapeLayer.strokeEnd > 0){
        _shapeLayer.strokeEnd = [[time.userInfo objectForKey:@"endStr"]  floatValue];
    }else{
        [self deleteTimer];
    }
}

- (void)deleteTimer{
    [self.timer invalidate];
    self.timer = nil;
}


@end

推荐阅读更多精彩内容