iOS 时钟动画

   在iOS开发中,定时器NSTimer并不能够准确的出发,通常使用NSTimer只能控制不需要精确处理的操作,而CADisplayLink就是在每次屏幕刷新时,通知系统。CADisplayLink最大的好处就是可以精准的在每次屏幕刷新时,设置屏幕的重绘!

  示例代码:

  

 1 #import "WKViewController.h"
 2 
 3 @interface WKViewController ()
 4 
 5 @end
 6 /**
 7  
 8  */
 9 @implementation WKViewController
10 {
11     CADisplayLink *_timer;
12 }
13 
14 - (void)viewDidLoad
15 {
16     [super viewDidLoad];
17     
18     _timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(snow)];
19     // 将时钟添加到主运行循环,才能够在每次屏幕刷新时工作
20    //每秒60次
21     [_timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
22 }
23 
24 
25 - (void)snow
26 {
27     //用于控制多少时间操作一次
28     static long counter = 0;
29     
30     counter++;
31     
32     if (counter % (15) == 0) {
33           //do something
34     }
35    
36 }
37 
38 
39 @end

 

posted @ 2014-11-01 17:33  pretty guy  阅读(209)  评论(0编辑  收藏  举报