随笔 - 400,  文章 - 0,  评论 - 7,  阅读 - 21万

 

 

1
2
3
4
5
6
7
8
@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

  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
@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   懂事长qingzZ  阅读(61)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示