CADisplayLink 的基本使用方法

标签: 

转载

分类: [iPhone开发]
[转载]CADisplayLink <wbr>的基本使用方法 [转载]CADisplayLink <wbr>的基本使用方法

自從 iOS SDK 3.1 起就增加了 CADisplayLink Class,這個 Class 的功能類似於 Timer。由於能支援每秒高達 60 fps 的畫面同步功能,所以更適合用在製作遊戲動畫上面,相較之下 Timer 較常使用在背景處理層面,其基本使用方式如下。(View-based Template)

第一步要先引入 CADisplayLink 的標頭檔,才能真正使用它。

1 #import <QuartzCore/CADisplayLink.h>

1
2
3
4
5
6
7
8
9
10
11
12
13
//自行定義的函式,用來設定使用CADisplayLink的相關參數
-(void)initializeTimer {
 
    //theTimer是CADisplayLink型態的指標,用來存放當前的設定狀態
    theTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(countTotalFrames)];
 
    //CADisplayLink內定值就是每秒60張(參數=1),參數=2就是每秒30張,以此類推
    double fps = 60 / theTimer.frameInterval;
    fpsLabel.text = [NSString stringWithFormat:@"%0.1f" , fps];
 
    //設定執行狀態並啟動theTimer
    [theTimer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
原文地址:https://www.cnblogs.com/monnRedShine/p/3134093.html