CADisplayLink的简单使用

CADisplayLink类似NSTimer是一个定时器,只不过是一秒会调用60次指定的方法

使用方法:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic, assign) int count;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 创建CADisplayLink
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkEvent)];
    
    [self performSelector:@selector(eventOne) withObject:nil afterDelay:1];
    
    // 一秒后销毁
    [self performSelector:@selector(eventTwo) withObject:nil afterDelay:2];
}

- (void)eventTwo
{
    [self.displayLink invalidate];
}

- (void)eventOne
{
    // 添加到循环圈(开始执行)
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)displayLinkEvent
{
    self.count++;
    NSLog(@"%i",self.count);
}

@end

 其中,displayLinkEvent方法在一秒内被调用了60次。

posted @ 2016-01-25 20:51  Rinpe  阅读(535)  评论(0编辑  收藏  举报