iOS-NSTimer 使用

1.NSTimer的创建方法

 

// 创建一个定时器,但没有添加到运行循环,我们需要在创建定时器后手动的调用 NSRunLoop 对象的 addTimer:forMode: 方法。
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo; 

// 创建一个timer并把它指定到一个默认的runloop模式中,并且在 TimeInterval时间后 启动定时器 
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo; 

// 创建一个定时器,但是么有添加到运行循环,我们需要在创建定时器后手动的调用 NSRunLoop 对象的 addTimer:forMode: 方法。
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo; 

// 创建一个timer并把它指定到一个默认的runloop模式中,并且在 TimeInterval时间后 启动定时器 
+ (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 NS_DESIGNATED_INITIALIZER; 

2. NStimer的开启

 

// 启动 Timer 触发Target的方法调用,但并不会改变Timer的时间设置。 即 time没有到达到,Timer会立即启动调用方法且没有改变时间设置,当时间 time 到了的时候,Timer还是会调用方法。
- (void)fire;

// 设置定时器的启动时间,常用来管理定时器的启动与停止 
@property (copy) NSDate *fireDate; 

timer.fireDate = [NSDate distantPast];  // 开启定时器
timer.fireDate = [NSDate distantFuture]; // // 停止定时器
[timer setFireDate:[NSDate date]]; // 继续定时器

3. NStimer的停止

 

// 获取定时器是否有效
@property (readonly, getter=isValid) BOOL valid;
// 停止 Timer,即将定时器设置成无效 ---> 将定时器从循环池中移除
- (void)invalidate;

4. NStimer的其他属性

 

// 这个是一个只读属性,获取定时器调用间隔时间
@property (readonly) NSTimeInterval timeInterval;

// 这是7.0之后新增的一个属性,因为NSTimer并不完全精准,通过这个值设置误差范围
@property NSTimeInterval tolerance NS_AVAILABLE(10_9, 7_0);

5. NStimer的内存释放

如果我们启动了一个定时器,在某个界面释放前,将这个定时器停止,甚至置为nil,都不能使这个界面释放,原因是系统的循环池中还保有这个对象。

6. NStimer使用注意

参数repeats是指定是否循环执行,YES将循环,NO将只执行一次。

timerWithTimeInterval这两个类方法创建出来的对象如果不用 addTimer: forMode方法手动加入主循环池中,将不会循环执行。

scheduledTimerWithTimeInterval 这两个方法会将定时器添加到当前的运行循环,运行循环的模式为默认模式。如果需要第一次就执行一次,需要调用开启定时器的方法。

init方法需要手动加入循环池,它会在设定的启动时间启动。

在页面释放前先释放定时器。

7.NSTimer为什么需要在RunLoop中才会有作用

NSTimer其实也是一种事件,而所有的source(事件)如果要起作用,必须添加到runloop中,并且此runloop是有效的,并运行着。

同理timer这种source(事件)要想起作用,那肯定也需要加到runloop中才会有效。

如果一个runloop里面不包含任何source(事件)的话,运行该runloop时会处于一种休眠状态等待下一个事件。所以如果创建了timer但是不添加runloop的话,timer资源处于一种闲置等待状态。

 

//创建timer
_requestTimer = [NSTimer scheduledTimerWithTimeInterval:requestInterval target:self selector:@selector(requestDriverPosition) userInfo:nil repeats:YES];
// 启动timer
_requestTimer.fireDate = [NSDate distantPast];  


//移除timer
- (void)removeTimer {
    if (_requestTimer) {
    //停止定时器
        _requestTimer.fireDate = [NSDate distantFuture];
        [_requestTimer invalidate];
        _requestTimer = nil;
    }
}



作者:良人不归_墨染锦年
链接:https://www.jianshu.com/p/494a252fb80d
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值