定时器的使用NStimer 和CADisplayLink

来源:互联网 发布:mac 删除应用程序数据 编辑:程序博客网 时间:2024/05/23 07:25

一,NSTimer

     scheduled 计划,安排

     interval : 间隔

     target :  一般指控制器

     selector: 方法

     userInfo : 用户自定义的参数

     repeats: 重复

     

     每隔1秒钟调用 控制器的  didClickButton方法, 传递的参数为nil

     

     一旦创建就会立即生效

     

     在使用timer的时候,如果调用了 invalidate方法,那么这个计时器就不会再次生效

     重新创建新的timer

     

    _timer = [NSTimerscheduledTimerWithTimeInterval:1

                                              target:self

                                            selector:@selector(didClickButton:)

                                            userInfo:nil

                                             repeats:YES];

    

//    [_timer fire];  调用fire 这个计时器会立即执行, 不会等待 interval 设置的时间

    //把图片轮播的NStime的优先级提高

    NSRunLoop *mainLoop = [NSRunLoopmainRunLoop];

    

    //将定时器添加到消息循环中

    [mainLoop addTimer:_timerforMode:NSRunLoopCommonModes];

    


二,CADisplaylink
        CADisplayLink是一种屏幕刷新频率的触发时钟机制,每秒钟执行60次左右,是一个计时器,可以使绘图的代码与视图的刷新频率保持同步,是专为动画定制的.

    CADisplayLink * link = [CADisplayLinkdisplayLinkWithTarget:selfselector:@selector(fire)];

    //添加到主运行循环

    [link addToRunLoop:[NSRunLoopmainRunLoop] forMode:NSDefaultRunLoopMode];





0 0
原创粉丝点击