iOS定时器

相关类:NSTimer

A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer.

Timer不是实时的,依赖于运行的loop,精度为50-100ms。如果run loop没有monitoring timer或者正在long callout,Timer不会被触发。

创建定时器

A timer object can be registered in only one run loop at a time, although it can be added to multiple run loop modes within that run loop. There are three ways to create a timer:scheduledTimerWithTimeInterval、timerWithTimeInterval和initWithFireDate

后两者在创建之后需要调用addTimer:forMode:

Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A non-repeating timer invalidates itself immediately after it fires.you should always call the invalidate method from the same thread on which the timer was installed. Invalidating the timer immediately disables it so that it no longer affects the run loop.

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:100 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];


userInfo:The user info for the timer. The timer maintains a strong reference to this object until it (the timer) is invalidated. This parameter may be nil.

selector原型: -(void)timerFireMethod:(NSTimer*)timer


启停

暂停:[timer setFireDate:[NSDate distantFuture]]

恢复:[timer setFireDate:[NSDate date]]


转载于:https://my.oschina.net/shanlilaideyu/blog/362272

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值