Files
real-e-party-iOS/YuMi/Tools/CountDown/CountDownHelper.m
edwinQQQ a35a711be6 chore: Initial clean commit
- Removed YuMi/Library/ (138 MB, not tracked)
- Removed YuMi/Resources/ (23 MB, not tracked)
- Removed old version assets (566 files, not tracked)
- Excluded Pods/, xcuserdata/ and other build artifacts
- Clean repository optimized for company server deployment
2025-10-09 16:19:14 +08:00

133 lines
4.2 KiB
Objective-C

//
// CountDownHelper.m
// xplan-ios
//
// Created by 冯硕 on 2021/9/15.
//
#import "CountDownHelper.h"
@interface CountDownHelper ()
@property (strong, nonatomic) dispatch_source_t timer;
@property (strong, nonatomic) dispatch_source_t clockwiseTimer;
@end
@implementation CountDownHelper
-(void)dealloc{
[self stopCountDown];
}
-(instancetype)init{
self = [super init];
if(self){
self.isCountdownFinish = YES;
self.seconds = 1.0;
}
return self;
}
+ (instancetype)shareHelper {
static dispatch_once_t onceToken;
static CountDownHelper * helper = nil;
dispatch_once(&onceToken, ^{
helper = [[CountDownHelper alloc] init];
});
return helper;
}
// 停止倒计时
- (void)stopCountDown {
if (self.timer != nil) {
dispatch_source_cancel(self.timer);
self.timer = nil;
}
self.isCountdownFinish = YES;
}
- (void)stopClockwise {
if (self.clockwiseTimer != nil) {
dispatch_source_cancel(self.clockwiseTimer);
self.clockwiseTimer = nil;
}
self.isCountdownFinish = YES;
}
// 开始倒计时
- (void)openCountdownWithTime:(int)totalTime{
if (time <= 0) {
return;
}
__block CGFloat time = (CGFloat)totalTime; //倒计时时间
if (self.timer != nil) {
dispatch_source_cancel(self.timer);
}
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(self.timer,dispatch_walltime(NULL, 0),1*NSEC_PER_SEC, 0); //每秒执行
__weak typeof(self) weakself = self;
dispatch_source_set_event_handler(self.timer, ^{
__strong typeof(weakself) self = weakself;
if(time <= 0){ //倒计时结束,关闭
dispatch_source_cancel(self.timer);
dispatch_async(dispatch_get_main_queue(), ^{
self.isCountdownFinish = YES;
if (self.delegate && [self.delegate respondsToSelector:@selector(onCountdownFinish)]) {
[self.delegate onCountdownFinish];
}
});
}else{
time--;
self.isCountdownFinish = NO;
dispatch_async(dispatch_get_main_queue(), ^{
//设置按钮显示读秒效果
if (self.delegate && [self.delegate respondsToSelector:@selector(onCountdownOpen:)]) {
[self.delegate onCountdownOpen:time];
}
});
}
});
dispatch_resume(self.timer);
}
- (void)openClockwiseWithTime:(int)totalTime{
if (totalTime <= 0) {
return;
}
__block CGFloat backTime = 0; //倒计时时间
if (self.clockwiseTimer != nil) {
dispatch_source_cancel(self.clockwiseTimer);
}
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
self.clockwiseTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(self.clockwiseTimer,dispatch_walltime(NULL, 0),self.seconds*NSEC_PER_SEC, 0); //每秒执行
__weak typeof(self) weakself = self;
dispatch_source_set_event_handler(self.clockwiseTimer, ^{
__strong typeof(weakself) self = weakself;
if(backTime > totalTime){ //倒计时结束,关闭
dispatch_source_cancel(self.clockwiseTimer);
dispatch_async(dispatch_get_main_queue(), ^{
self.isCountdownFinish = YES;
if (self.delegate && [self.delegate respondsToSelector:@selector(onClockwiseFinish)]) {
[self.delegate onClockwiseFinish];
}
});
}else{
backTime = backTime + self.seconds;
self.isCountdownFinish = NO;
dispatch_async(dispatch_get_main_queue(), ^{
//设置按钮显示读秒效果
if(self.delegate && [self.delegate respondsToSelector:@selector(onClockwiseOpen:)]){
[self.delegate onClockwiseOpen:backTime];
}
});
}
});
dispatch_resume(self.clockwiseTimer);
}
@end