@interface SHMaasPTOnlineCarMatchmakingCircleCountdownView : UIView
@property (nonatomic, assign) NSInteger countdownDuration; // 倒计时总秒数
@property (nonatomic, assign) NSInteger currentCountdown; // 当前剩余秒数
@property (nonatomic, strong) UILabel *timeLabel; // 显示倒计时的标签
- (void)startCountdownFrom:(NSInteger)initialSeconds;
///取消倒计时
- (void)cancelCountdown;
@end

  

 

@interface SHMaasPTOnlineCarMatchmakingCircleCountdownView()
@end

@implementation SHMaasPTOnlineCarMatchmakingCircleCountdownView {
    UIBezierPath *_circlePath;
    CAShapeLayer *_circleLayer;
    CAShapeLayer *_bgLayer;
    dispatch_source_t _countdownTimer;
    dispatch_source_t _countdownTimer2;
}

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
}

- (instancetype)init {
    self = [super init];
    if (self) {
//        self.backgroundColor = UIColor.redColor;

        // 添加时间标签
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 30)];
        _timeLabel.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
        _timeLabel.textColor = [UIColor colorWithHexString:@"#121612"];
        _timeLabel.font = [UIFont systemFontOfSize:16 weight:(UIFontWeightMedium)];
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_timeLabel];
        [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
    }
    return self;
}

- (void)addLayer{

    UIBezierPath *path = [UIBezierPath bezierPath];
    // 添加一个半圆形(以view的中心为圆心)
    CGFloat radius = MIN(self.bounds.size.width, self.bounds.size.height) / 2;
    ///设置起始点 为顶部
    [path addArcWithCenter:self.center radius:radius startAngle: 1.5 * M_PI endAngle: 3.5 * M_PI clockwise:YES];
    
    ///灰色背景CAShapeLayer
    CAShapeLayer  *bgLayer  =  [CAShapeLayer  layer];
    bgLayer.frame  =  self.bounds;
    bgLayer.strokeEnd  =  1.0;//结束点
    bgLayer.strokeStart  =  0.0;//开始点
    bgLayer.path  =  path.CGPath;
    bgLayer.fillColor = [UIColor clearColor].CGColor; //Layer 的填充色
    bgLayer.lineWidth  =  5.0f;//宽度
    bgLayer.strokeColor  =  [UIColor  colorWithHexString:@"#DCDDE0"].CGColor;//边框色
    //将CAShaperLayer放到某个层上显示
    [self.layer  addSublayer:bgLayer];
    
    ///绿色CAShapeLayer
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = self.bounds;
//    shapeLayer.strokeEnd  =  1.0;//结束点
//    shapeLayer.strokeStart  =  0.0;//开始点
    shapeLayer.path = path.CGPath;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;//Layer 的填充色
    shapeLayer.lineCap = kCALineCapRound;///圆角
    shapeLayer.lineWidth = 5.0f;//宽度
    shapeLayer.strokeColor = [UIColor colorWithHexString:@"#33A634"].CGColor;//边框色
    [self.layer addSublayer:shapeLayer];
    
    ///绿色CAShapeLayer 添加 动画
    CABasicAnimation *pathAnima = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnima.duration = 5.0f;//动画时间
    pathAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pathAnima.fromValue = [NSNumber numberWithFloat:0.0f];//开始点
    pathAnima.toValue = [NSNumber numberWithFloat:1.0f];//结束点
    pathAnima.fillMode = kCAFillModeForwards;
    pathAnima.removedOnCompletion = NO;
    [shapeLayer addAnimation:pathAnima forKey:@"strokeEndAnimation"];
}
    
- (void)startCountdownFrom:(NSInteger)initialSeconds {
    [self addLayer];
    [self setNeedsDisplay];
    _countdownDuration = initialSeconds + 0.5;///有时差,
    __weak typeof(self) weakSelf = self;
    
    _currentCountdown = _countdownDuration;
    _countdownTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    dispatch_source_set_timer(_countdownTimer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC); // 每秒更新一次
    dispatch_source_set_event_handler(_countdownTimer, ^{
        weakSelf.currentCountdown-= 1;
        [weakSelf updateTimeLabel];
        if (weakSelf.currentCountdown <= 0) {
            dispatch_source_cancel(self->_countdownTimer);
            // 倒计时结束后的处理
        }
    });
    dispatch_resume(_countdownTimer);
}

- (void)cancelCountdown {
    if (_countdownTimer) {
        dispatch_source_cancel(_countdownTimer);
        _countdownTimer = nil;
    }
    _currentCountdown = _countdownDuration; // 取消后重置为初始值
    [self updateTimeLabel]; // 更新时间标签显示为初始值
//    _circleLayer.strokeEnd = 1.0; // 清除环形进度条填充
}

- (void)updateTimeLabel {
    NSInteger minutes = _currentCountdown / 60;
    NSInteger seconds = _currentCountdown % 60;
    _timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld", (long)minutes, (long)seconds];
}

@end

  

参考:

https://www.jianshu.com/p/887c9afadb02

posted on 2024-01-27 20:34  懂事长qingzZ  阅读(53)  评论(0编辑  收藏  举报