不懂iOS如何实现简单易用的GCD计时器??其实想解决这个问题也不难,下面让小编带着大家一起学习怎么去解决,希望大家阅读完这篇文章后大所收获。
在学习iOS过程中,想定大家对于定时器都不陌生,在日常开发中总会碰到需要计时器的功能,常见的定时器有NSTimer、GCD、CADisplayLink。网上也有很多的教程介绍三者的区别,主要讲的是GCD这种方式使用以及封装。
三者概括区别
优点 | 缺点 | |
---|---|---|
NSTimer | 使用简单 | 受Runloop影响会导致计时不精准 |
CADisplayLink | 精度高      | CPU负载的时候会影响触发事件,且触发事件大于触发间隔会导致掉帧现象。 |
GCD | 较精准 | 代码较多,基本不受其他影响 |
总结:NSTimer和CADisplayLink易受影响,而GCD虽然代码多,但是可控性非常强。
GCD
/** 获取一个全局的线程来运行计时器*/
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
/** 创建一个计时器*/
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
/** 设置计时器, 这里是每10毫秒执行一次*/
dispatch_source_set_timer(timer, dispatch_walltime(nil, 0), 10*NSEC_PER_MSEC, 0);
/** 设置计时器的里操作事件*/
dispatch_source_set_event_handler(timer, ^{
//do you want....
});
开启、继续已暂停的定时器
dispatch_resume(timer);
暂停定时器
/** 挂起的时候注意,多次暂停的操作会导致线程锁的现象,即多少次暂停,
* 对应多少次的继续操作,即dispatch_suspend和dispatch_resume
* 是成对出现的,计时器才会继续工作。
*/
dispatch_suspend(timer);
结束定时器
dispatch_source_cancel(timer);
构思封装
写代码之前构思好功能模块以及会遇到的问题的解决方案、代码逻辑,再来下手写代码,会有事半功倍的效果。
部分代码
/** app进入后台*/
- (void)appDidEnterBackground{
[self suspend];
NSDate *date = [[NSDate alloc] init];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"yyyy-MM-dd HH:mm:ss:SSS";
self.appDidEnterBackgroundTime = [date timeIntervalSince1970];
}
/** app进入前台*/
- (void)appDidEnterForeground{
NSDate *date = [[NSDate alloc] init];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
self.appDidEnterForegroundTime = [date timeIntervalSince1970];
[self reCalculateMinder];
}
/** 不失精度加减乘除计算结果*/
- (NSDecimalNumber *)value: (NSTimeInterval)value
byOpration: (OMDecimalOprationType)byOpration
percision: (NSInteger)percision
withValue: (NSTimeInterval)withValue{
NSDecimalNumber *number = [self numberValueWithString: value];
NSDecimalNumber *withNumber = [self numberValueWithString: withValue];
NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode: NSRoundPlain scale: percision raiseOnExactness: NO raiseOnOverflow: NO raiseOnUnderflow: NO raiseOnDivideByZero: YES];
switch (byOpration) {
case OMDecimalOprationTypeAdd:
return [number decimalNumberByAdding: withNumber withBehavior:handler];
break;
case OMDecimalOprationTypeSubtract:
return [number decimalNumberBySubtracting: withNumber withBehavior: handler];
break;
case OMDecimalOprationTypeDivide:
return [number decimalNumberByDividingBy: withNumber withBehavior: handler];
break;
case OMDecimalOprationTypeMultiple:
return [number decimalNumberByMultiplyingBy: withNumber withBehavior: handler];
break;
default:
break;
return nil;
}
@property (nonatomic, strong) OMTimer *timer;
self.timer = [[OMTimer alloc] init];
self.timer.timerInterval = 30;
self.timer.precision = 100;
self.timer.isAscend = NO;
self.timer.progressBlock = ^(OMTime *progress) {
NSLog(@"%@:%@:%@:%@", progress.hour, progress.minute, progress.second, progress.millisecond;
};self.timer.completion = ^{
NSLog(@"complete done!");
};
Swift版本
最近喜欢上了OC,如有小伙伴需要Swift的版本的话可以留言或者私我,可以在写个Swift版本,:stuck_out_tongue_winking_eye:。
感谢你能够认真阅读完这篇文章,希望小编分享iOS如何实现简单易用的GCD计时器?内容对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。