美文网首页你可不能输!iOS就是ios用的
创建一个只在滑动模式下计时的定时器

创建一个只在滑动模式下计时的定时器

作者: __微凉 | 来源:发表于2015-08-10 00:55 被阅读517次

创建定时器

在NSTimer类中有几种创建定时器的方法:


 // 类方法
 + (NSTimer *)timerWithTimeInterval:target:selector:userInfo:repeats:

 + (NSTimer *)scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
 
 // 实例方法
 - (instancetype)initWithFireDate:interval:target:selector:userInfo:repeats:
                                    

要想让定时器真正的工作,需要把定时器加入到RunLoop中,需要注意的是,在子线程的RunLoop中添加定时器需要自己创建一个和该子线程关联的RunLoop.当你在子线程中调用[NSRunLoop currentRunLoop]时,会帮你创建一个RunLoop.

// 实例方法创建定时器
NSDate *futureDate = [NSDate dateWithTimeIntervalSinceNow:1.0];
NSTimer *timer = [[NSTimer alloc] initWithFireDate:futureDate 
                                          interval:0.1 
                                            target:self
                                          selector:@selector(calledSelector:)
                                          userInfo:nil   
                                           repeats:YES];

[[NSRunLoop currentRunLoop] timer forMode:NSDefaultRunLoopMode];


// timerWithTimeInterval类方法创建定时器 
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 
                                         target:self 
                                       selector:@selector(calledSelector:) 
                                       userInfo:nil 
                                        repeats:YES];
                                                 
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];


// scheduledTimerWithTimeInterval类方法创建对象
[NSTimer scheduledTimerWithTimeInterval:0.2
                                 target:self
                               selector:@selector(calledSelector:)
                               userInfo:nil
                                repeats:YES];

在上面几个方法中,只有scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:方法不需要将timer添加到NSRunLoop上面,因为这个方法默认将timer添加到了NSRunLoop上,并且默认是NSDefaultRunLoopMode.而另外两个方法需要手动添加到NSRunLoop并指定模式.

当不想添加到默认模式的时候可以使用第二种方式创建定时器对象.这个需求通常在滚动UI的时候,想要使定时器继续工作,便需要把timer添加到NSRunLoopNSRunLoopCommonModes上.
NSRunLoopCommonModes是一组可配置的通用模式,对于Cocoa应用,该模式缺省的包含了default,modal以及event tracking(即UITrackingRunLoopMode)模式.

开启一个NSTimer实质上是在当前的RunLoop中配置了一个定时源.每次循环都会检测timer是否可以出发,当RunLoop处于A模式,注册在B模式的timer就无法被检测到了.因此当滑动一个scrollView时,注册在NSDefaultRunLoopMode上的timer是无法被UITrackingRunLoopMode模式检测到的.因此在UITrackingRunLoopMode模式下也要注册一下timer,才能让定时器依照我们的期望正常工作.

如果我想用scheduledTimerWithTimeInterval类方法创建一个在滑动模式下计时的定时器,该怎么做呢?

由于scheduledTimerWithTimeInterval:target:selector:userInfo:repeats默认将定时器配置到了NSRunLoop上,并且默认是NSDefaultRunLoopMode,想要在滑动模式下也能计时,需要使用addTimer:forMode:方法添加滑动换模式.


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

[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
// 或者[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

很简单~再看一个问题.

如果我想用scheduledTimerWithTimeInterval类方法创建一个在滑动模式下计时的定时器,该怎么做呢?

注意,这个问题比上一个问题多了一个字.这下麻烦了,scheduledTimerWithTimeInterval:target:selector:userInfo:repeats方法默认添加到了NSDefaultRunLoopMode,头文件里面也没有提供更改或删除mode的方法.如果我们使用[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];,实际上是让这个timer配置到了两种mode下.如何删除其中一个mode呢? 我使用了XtracedumpNSTimer的私有方法,成功地将timerNSDefaultRunLoopMode中移除.



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

[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];

[[NSRunLoop currentRunLoop] performSelector:@selector(removeTimer:forMode:)  
                            withObject:timer
                            withObject:NSDefaultRunLoopMode];

虽然这操作然并卵,但是对理解RunLoop还是有帮助的.

相关文章

  • 创建一个只在滑动模式下计时的定时器

    创建定时器 在NSTimer类中有几种创建定时器的方法: 要想让定时器真正的工作,需要把定时器加入到RunLoop...

  • 图片自动播放(UIScrollView+UIPageContro

    创建UIScrollView UIPageControl 添加定时器 手动滑动与自动滑动的互相转换(UIScrol...

  • iOS 关于定时器和tableview刷新

    有个需求是在首页显示一个时间的倒计时,计算了时间差,用定时器做了倒计时。后来发现bug,首页滑动到最下边的时候会自...

  • RunLoop应用(二)

    控制定时器在特定模式下执行(不常用) 当需要显示的图片特别多时,如果同时滑动表格,界面会有卡顿的感觉这时可以设置在...

  • 10.12总结

    今天老师讲了定时器,其中通用定时器预分频系数1~65536,模式为向上,向下,向上/向下计数模式。在运用到计时程序...

  • iOS - GCD 实现定时器、倒计时

    标签(空格分隔): 计时器 GCD timer 倒计时 GCD 定时器 NSTimer 的定时器是在 RunLoo...

  • 08.1-Runloop的实际应用场景

    我们在平时开发过程中涉及到runloop相关的应用场景大致有如下几种: NSTimer创建的定时器在滑动过程中失效...

  • iOS开发-GCD多线程来写倒计时

    今天给各位猿友说一下怎么使用GCD来实现倒计时的功能效果,首先给大家简单介绍一下实现的原理:使用GCD创建定时器并...

  • Qnx定时器的使用

    1.创建定时器 QNX定时器首先需要创建一个定时器对象,设置通知类型,一般包括信号、脉冲或线程创建,并创建通知结构...

  • 命题-第十三章-简答

    定时器模式与计时器模式的区别是什么? 【解析】 定时工作模式和技术工作模式的工作原理相同,只是计数脉冲来源有所不同...

网友评论

    本文标题: 创建一个只在滑动模式下计时的定时器

    本文链接:https://www.haomeiwen.com/subject/swhiqttx.html