定时器

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

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


//声明定时器

@property (nonatomic,strong) NSTimer *timer;

/**
* 添加定时器
*/
- (void)addTimer
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
//NSRunLoop可以优先级处理些定时器(线程优先)
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

/**
* 移除定时器
*/
- (void)removeTimer
{
[self.timer invalidate];//设置定时器无效
    self.timer = nil;
}

调用代理时 定时器 事例:


#pragma  mark -代理方法

/**
当scrollView正在滚动就会调用 */
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//根据scrollView的滚动位置决定pageControl显示第几页
CGFloat scrollW=self.scrollView.frame.size.width;
int page=(scrollView.contentOffset.x+scrollW*0.5)/scrollW;
self.pageControl.currentPage=page;
} /**
开始拖拽的时候调用
*/ -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//停止定时器(一旦定时器停止了,就不能再使用)
[self removeTimer];
} /**停止拖拽的时候调用*/
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
//开启定时器
[self addTimer];
}

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. bs4的简单使用

    一.使用流程 解析流程: 1.pip install bs4 2.导包:from bs4 import BeautifulSoup 3.实例化一个BeautifulSoup对象(将页面源码数据加载到该 ...

  2. redis初识及基本操作

    一.redis介绍: 1.Redis是什么: REmote DIctionary Server(远程字典服务器) 是完全开源免费的,用C语言编写的,遵守BSD协议,是一个高性能的(Key-Value) ...

  3. 安装openstack时遇到的错误

    学习opensatck的第一步是安装DevStack来进行本机操作 1. 下面命令没有权限,解决办法:切换到root用户下执行sudo -s echo "stack ALL=(ALL) NO ...

  4. Hive 报错信息及解决方法

    return code 2 为SQL报错. return code 1 一般为权限问题. 具体要看源码.

  5. kafka删除主题数据和删除主题

    1.删除主题 在server.properties中增加设置,默认未开启 delete.topic.enable=true 删除主题命令 /bin/kafka-topics --delete --to ...

  6. GM MDI Tech 3 VS GM tech 2

    Many customers ask for this question: what is the difference between GM tech 2 and GM MDI Tech 3 sca ...

  7. 转 oracle 默认自动统计信息 时间修改

    ############sql3: https://blog.csdn.net/dataminer_2007/article/details/41363417http://blog.51cto.com ...

  8. Tomcat在处理GET和POST请求时产生的乱码问题

    最近一直在做关于Servlet的事情,常常出现乱码,很是烦人,处理乱码的方法有时候有效,有时候没有效果,今天抽个时间小结一下,以防以后再出现这种问题. 一般的处理乱码的方式都是用: request.s ...

  9. 进入BIOS的步骤

    查看自己的BIOS版本 $ systeminfo 对号入座,电脑进入BIOS的两种实用方法

  10. pyplot

    错误: 执行 import matplotlib.pyplot 报错 ImportError: No module named _tkinter, please install the python- ...