定时器

CADisplayLink:时间间隔比较小(使用时间频率高)的时候用(适合小游戏中开发)

NSTimer:时间间隔比较大的时候调用(适合图片轮放的时候用)

  1.  

//声明定时器

@property (nonatomic,strong) NSTimer *timer;

  1. /**
  2. * 添加定时器
  3. */
  4. - (void)addTimer
  5. {
  6. self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
  7. //NSRunLoop可以优先级处理些定时器(线程优先)
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
  1. }
  2.  
  3. /**
  4. * 移除定时器
  5. */
  6. - (void)removeTimer
  7. {
  8. [self.timer invalidate];//设置定时器无效
  1. self.timer = nil;
  2. }

调用代理时 定时器 事例:

  1.  

#pragma  mark -代理方法

  1. /**
  2. 当scrollView正在滚动就会调用
  3.  
  4. */
  5. -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
  6. //根据scrollView的滚动位置决定pageControl显示第几页
  7. CGFloat scrollW=self.scrollView.frame.size.width;
  8. int page=(scrollView.contentOffset.x+scrollW*0.5)/scrollW;
  9. self.pageControl.currentPage=page;
  10. }
  11.  
  12. /**
  13. 开始拖拽的时候调用
  14. */
  15.  
  16. -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  17. {
  18. //停止定时器(一旦定时器停止了,就不能再使用)
  19. [self removeTimer];
  20. }
  21.  
  22. /**停止拖拽的时候调用*/
  23. -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  24. {
  25. //开启定时器
  26. [self addTimer];
  27. }

IOS 添加定时器(NSTimer)的更多相关文章

  1. [置顶] ios 时间定时器 NSTimer应用demo

    原创文章,转载请注明出处:http://blog.csdn.net/donny_zhang/article/details/9251917 demo功能:ios NSTimer应用demo .ipho ...

  2. IOS中定时器NSTimer的开启与关闭

    调用一次计时器方法: myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scro ...

  3. 【转】iOS中定时器NSTimer的使用

    原文网址:http://www.cnblogs.com/zhulin/archive/2012/02/02/2335866.html 1.初始化 + (NSTimer *)timerWithTimeI ...

  4. iOS中定时器NSTimer的使用-备用

    1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...

  5. 【转】IOS中定时器NSTimer的开启与关闭

    原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...

  6. 【转】 IOS中定时器NSTimer的开启与关闭

    原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...

  7. 蜗牛爱课- iOS中定时器NSTimer使用

    调用一次计时器方法: //不重复,只调用一次.timer运行一次就会自动停止运行 self.locationTimer = [NSTimer  target:self selector: @selec ...

  8. iOS中定时器NSTimer的使用/开启与关闭

      一.只调用一次计时器方法: //不重复,只调用一次.timer运行一次就会自动停止运行 myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5  ...

  9. ios 中定时器:NSTimer, CADisplayLink, GCD

    #import "ViewController.h" #import "RunloopViewController.h" @interface ViewCont ...

随机推荐

  1. TensorFlow入门测试程序

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist=input_data. ...

  2. hive distcp数据同步

    -- 同步HDFS数据(shell执行) hadoop distcp \ -Dmapred.job.queue.name=queue_name \ -update \ -skipcrccheck hd ...

  3. python解决excel工作薄合并处理(openpyxl处理excel2010以上版本)

    前段时间使用xlrd.xlwt对文件进行处理(https://www.cnblogs.com/pinpin/p/10287491.html),但是只能处理excel2010以下版本,所以又写了个处理e ...

  4. ORACLE CBC LATCH 检查

    ###############1.DB meet latch: cache buffers chains event from awr report ,check latch: cache buffe ...

  5. str 操作方法

    # str 类,字符串 # name ='alex' # 首字母变大写 # test ='alex' # v= test.capitalize() # print(v) # # 大写全部变小写 # t ...

  6. Trees on the level UVA - 122 (二叉树的层次遍历)

    题目链接:https://vjudge.net/problem/UVA-122 题目大意:输入一颗二叉树,你的任务是按从上到下,从左到右的顺序输出各个结点的值.每个结点都按照从根节点到它的移动序列给出 ...

  7. visual stdio使用

    现在转换使用visual stdio,因为很多和以前的快捷键不同,也不打算换了,这样可移动性应该更好吧. 1.注释代码, 首先按下ctrl + k,然后再按下ctrl + c 2.取消注释,首先按下c ...

  8. Linux下判断磁盘是SSD还是HDD的几种方法

    环境介绍 Fedora release 25 (Twenty Five) 判断方法 方法一 判断cat /sys/block/*/queue/rotational的返回值(其中*为你的硬盘设备名称,例 ...

  9. pat05-图1. List Components (25)

    05-图1. List Components (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue For a ...

  10. wait/notify

    某面试题,实现一个生产者——消费者模型 题目:采用多线程技术,通过wait/notify,设计实现一个符合生产者和消费者问题的程序,对某一个对象(枪膛)进行操作,其最大容量是20颗子弹,生产者线程是一 ...