在现在很多 app 中,我们经常会看到轮播图,轮播广告等等,比如淘宝、京东商城 app,他们都可以定时循环地播放广告、图片,背后的功臣之一就是今天的主角——定时器 NSTimer。
简单地介绍了它的应用场景,接下来,说说此次要分享的技能点:
### 定时开始:
我创建一个
,然后让他成为导航控制器的HomeViewController
,在RootViewController
的HomeViewController
调用定时器方法,如下代码:viewDidLoad
#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self regularTime];
}
/*
定时器的常规用法
*/
- (void)regularTime
{
//自动开启
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}
- (void)timerAction
{
NSLog(@"定时器:%s",__func__);
}
@end
运行结果:每隔一秒就打印一次。
还有另外一种方式也可以开启定时器,那就是调用这个方法:
+ (NSTimer * ) timerWithTimeInterval: (NSTimeInterval) ti target: (id) aTarget selector: (SEL) aSelector userInfo: (nullable id) userInfo repeats: (BOOL) yesOrNo;
** 注意 1:** 单独地写这一句代码,默认是不会开启定时器的,让我们看看苹果官方原文是怎么说的:"
" 也就是说,需要添加到 runloop 中,手动开启。运行的结果同上。You must add the new timer to a run loop, using addTimer:forMode:. Then, after ti seconds have elapsed, the timer fires, sending the message aSelector to target. (If the timer is configured to repeat, there is no need to subsequently re-add the timer to the run loop.)
** 注意 2:** 在主线程中,runloop 是默认开启,如果是在子线程中开启开启定时器,那么我们还需要手动开启 runloop。运行的结果同上。
注意 3: NSRunLoopCommonModes 和 NSDefaultRunLoopMode 优先级使用场景不同:一般都是默认模式。
scheduledTimerWithTimeInterval
方法时,此时 Timer 会被加入到当前线程的 RunLoop 中,且模式是默认的 NSDefaultRunLoopMode
,如果当前线程就是主线程,也就是 UI 线程时,某些 UI 事件,比如 UIScrollView 的拖动操作,会将 RunLoop 切换成 NSEventTrackingRunLoopMode
模式,在这个过程中,默认的 NSDefaultRunLoopMode
模式中注册的事件是不会被执行的,也就是说,此时使用 scheduledTimerWithTimeInterval
添加到 RunLoop 中的 Timer 就不会执行。NSRunLoopCommonModes
,这个模式等效于 NSDefaultRunLoopMode
和 NSEventTrackingRunLoopMode
的结合。(参考官方文档)
代码如下所示:- (void)viewDidLoad {
[super viewDidLoad];
//在主线程中开启定时器
[self regularTime];
//在子线程中开启定时器
// [NSThread detachNewThreadWithBlock:^{
// NSLog(@"%@",[NSThread currentThread]);
// [self regularTime];
// }];
}
- (void)regularTime
{
timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
//runloop中添加定时器
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
//在子线程中启用定时器必须开启runloop
// [[NSRunLoop currentRunLoop] run];
}
- (void)timerAction
{
NSLog(@"定时器:%s",__func__);
}
####fire 方法
简单说说 fire 方法,fire 是火焰的意思,从字面意思可以联想到燃料、加速的意思,that's right!
——> 就是加速计时的意思,我们通过比如点击事件,来让定时器人为地加速计时,这个比较简单,这里就不多赘述。[timer fire]
####GCD 定时器
GCD 定时器,通过创建队列、创建资源来实现定时的功能,如下代码所示: ** 注意:** 如果延迟 2 秒才开启定时器,那么
必须写在外面。dispatch_resume(gcdTimer)
#import "HomeViewController.h"
@interface HomeViewController ()
{
NSTimer * timer;
}
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self gcdTimer:1 repeats:YES];
}
- (void)gcdTimer:(int)timerInterVal repeats:(BOOL)repeat
{
//创建队列
dispatch_queue_t queue = dispatch_queue_create("my queue", 0);
//创建资源
dispatch_source_t gcdTimer = dispatch_source_create(&_dispatch_source_type_timer, 0, 0, queue);
dispatch_source_set_timer(gcdTimer,dispatch_time(DISPATCH_TIME_NOW, 0),1*NSEC_PER_SEC,0);
dispatch_source_set_event_handler(gcdTimer, ^{
if (repeat) {
NSLog(@"重复了");
[self timerAction];
} else
{
// [self timerAction];
// //调用这个方法,释放定时器
// dispatch_source_cancel(gcdTimer);
//延迟两秒会出现什么情况呢?
/*
为何会调用两次?2秒之后再触发定时器后,耽搁了0.001秒去cancel,那定时器已经再次
触发了,所以走了两次,解决的方法就是把cancel写在外面。
*/
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)
(timerInterVal*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self timerAction];
});
dispatch_source_cancel(gcdTimer);
}
});
dispatch_resume(gcdTimer);
}
/*
定时器的常规用法
*/
- (void)regularTime
{
//自动开启
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector
(timerAction) userInfo:nil repeats:YES];
}
- (void)timerAction
{
NSLog(@"定时器:%s",__func__);
}
##### 定时器循环引用的解决思路
- 循环引用出现的场景: eg:有两个控制器 A 和 B,A 跳转到 B 中,B 开启定时器,但是当我返回 A 界面时,定时器依然还在走,控制器也并没有执行 dealloc 方法销毁掉。
先说简单的解决思路: 苹果官方为了给我们解决这个循环引用的问题,提供了一个定时器的新的自带方法:
代码如下:+ (NSTimer * ) scheduledTimerWithTimeInterval: (NSTimeInterval) interval repeats: (BOOL) repeats block: (void( ^ )(NSTimer * timer)) block;
- (void)regularTime
{
//用苹果自带的方法,使用weakself就可以解决定时器循环引用问题
__weak typeof(self)weakSelf = self;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f repeats:YES block:^(NSTimer * _Nonnull timer) {
[weakSelf timerAction];
}];
}
第二种思路:
- (void)regularTime
{
//自动开启
//timer置为nil或者__weak typeof(self)weakself = self也无法解决定时器循环引用问题
__weak typeof(self)weakself = self;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:weakself selector:
@selector(timerAction) userInfo:nil repeats:YES];
}
既然如此,那该如何是好? 答案是:通过类扩展,自己改写 NSTimer 的类方法,在控制器中调用新的类方法,直接 show the code:
NSTimer+HomeTimer.h 中:
#import
@interface NSTimer (HomeTimer)
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)timerInterval block:(void(^)())block repeats:(BOOL)repeat;
+ (void)timerAction:(NSTimer *)timer;
@end
NSTimer+HomeTimer.m 中:
#import "NSTimer+HomeTimer.h"
@implementation NSTimer (HomeTimer)
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)timerInterval block:(void(^)())block repeats:(BOOL)repeat
{
return [self timerWithTimeInterval:timerInterval target:self selector:@selector(timerAction:) userInfo:block repeats:YES];
}
+ (void)timerAction:(NSTimer *)timer
{
void (^block)() = [timer userInfo];
if (block) {
block();
}
}
@end
类扩展写好之后,在控制器中调用,重写类方法,让定时器对 NSTimer 类强引用,类是没有内存空间的,就没有循环引用,跟苹果提供的新方法是类似的处理方式,如下代码和运行结果所示:
#import "HomeTimerViewController.h"
#import "NSTimer+HomeTimer.h"
@interface HomeTimerViewController ()
{
NSTimer * timer;
}
@end
@implementation HomeTimerViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self regularTime];
self.view.backgroundColor = [UIColor greenColor];
}
- (void)regularTime
{
__weak typeof(self)weakSelf = self;
timer = [NSTimer timerWithTimeInterval:1.0f block:^{
[weakSelf timerAction];
} repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
- (void)timerAction
{
NSLog(@"定时器:%s",__func__);
}
- (void)dealloc
{
NSLog(@"%s",__func__);
}
@end
### 定时结束:用时 2 小时 32 分钟 ### 总结:
我之前在开发 app 的时候,对定时器更多是会用的层次,经过这次的深入学习,对定时器的原理有了更深入的理解、认识,技术的提升,很多时候都是基础知识的延伸,对原理理解透彻,很多东西就可以举一反三,全部都通了,希望对自己和各位同道人有所帮助。
来源: https://juejin.im/post/5a30893d6fb9a0451a765d2e