一、初始化方法
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
- (instancetype)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(nullable id)ui repeats:(BOOL)rep
1、参数repeats是指定是否循环执行,YES将循环,NO将只执行一次。
2、timerWithTimeInterval这两个类方法创建出来的对象如果不用 addTimer: forMode方法手动加入主循环池中,将不会循环执行。所以需要调用addTimer: forMode方法, 调用就会启动fire.
3、scheduledTimerWithTimeInterval这两个方法自动加入当前runloop中(只是当前的runloop=NSDefaultRunLoopMode, 其他runloop mode未处理), 所以会自动fire.
4、init和timerWithTimeInterval一样, 都需要手动加入runloop,它会在设定的启动时间启动。
二、相关属性/方法
- (void)fire;//点燃,立即触发该定时器
@property (copy) NSDate *fireDate;//点燃日期,确定触发定时器的日期
@property (readonly) NSTimeInterval timeInterval;//只读属性,获取定时器调用间隔时间(如果循环的话)
@property NSTimeInterval tolerance;//设置误差范围, 毕竟NSTimer不完全精确 - (void)invalidate;//将定时器从runloop中移除
@property (readonly, getter=isValid) BOOL valid;//定时器是否有效
@property (nullable, readonly, retain) id userInfo;//其他参数信息
//再次开启定时器
[self.timer setFireDate:[NSDate distantPast]];
或者
[self.timer setFireDate:[NSDate date]];
//关闭定时器:
[self.timer setFireDate:[NSDate distantFuture]];
⚠️
1.App置于后台时, 最好将timer暂停, 否则会消耗资源(比如电量).
2.如果是在ViewController中处理的timer, 可以在其生命周期方法viewWillAppear
和viewWillDisappear
中将timer暂停或者重启.
三、应用, 对NSTimer的一个封装:(注意循环引用的问题)
.h中
@property (nonatomic, assign)CGFloat timerChangeInterval;///计时器计时间隔
@property (nonatomic, copy) void (^timerHandlerBlock)(CGFloat timerIntervalTime);
- (void)startTimer;
- (void)stopTimer;
- (void)pauseTimer;
.m中
@property (nonatomic, strong)NSTimer *timer;
- (NSTimer *)timer
{
if (!_timer) {
YYTextWeakProxy *weakProxy = [YYTextWeakProxy proxyWithTarget:self];
_timer = [NSTimer timerWithTimeInterval:self.timerChangeInterval target:weakProxy selector:@selector(timeChange) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
/**
scheduledTimerWithTimeInterval和上面两行效果一样.
_timer = [NSTimer scheduledTimerWithTimeInterval:self.timeChangeInterval target:weakProxy selector:@selector(timeChange) userInfo:nil repeats:YES];
*/
// //发生滚动事件时也计时
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:UITrackingRunLoopMode];
//加入到runloop会自动出发fire. 所以这里先暂停, startFire由业务触发
[_timer setFireDate:[NSDate distantFuture]];
}
return _timer;
}
- (void)stopTimer
{
if (_timer) {
[self.timer invalidate];//停止
if (self.timer) {
self.timer = nil;
}
}
}
- (void)pauseTimer
{
//暂停
if (_timer) {
[_timer setFireDate:[NSDate distantFuture]];
}
}
- (void)startTimer
{
[self.timer setFireDate:[NSDate date]];
}
- (void)timeChange
{
if (_timerHandlerBlock) {
_timerHandlerBlock(self.timerChangeInterval);
}
}
四、dispatch定时器
@property (nonatomic)dispatch_source_t dispatchTimer;
////创建定时器
if (!self.dispatchTimer) {
self.dispatchTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
dispatch_source_set_timer(self.dispatchTimer, DISPATCH_TIME_NOW, 1*NSEC_PER_SEC, 0*NSEC_PER_SEC);
dispatch_source_set_event_handler(self.dispatchTimer, ^{
//hanlde user action
});
dispatch_resume(self.dispatchTimer);
}
//挂起定时器
if (self.dispatchTimer) {
dispatch_suspend(self.dispatchTimer);
}
//释放定时器
if (self.dispatchTimer) {
dispatch_source_cancel(self.dispatchTimer);
self.dispatchTimer = nil;
}