In iOS development, the timer is not able to accurately NSTimer departure, usually NSTimer can only control the operation does not require precise handling, while CADisplayLink that every time the screen refreshes, the notification system. The greatest advantage is that you can CADisplayLink accurate at each screen refresh, set to redraw the screen!
Sample code:
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 // the clock cycle added to the main run, it is possible to work at each screen refresh 20 // 60 times per second 21 is [_timer addToRunLoop: [the NSRunLoop mainRunLoop] forMode: NSDefaultRunLoopMode]; 22 is } 23 is 24 25 - ( void ) Snow 26 { 27 // much time for controlling the operation of a 28 static Long counter = 0 ; 29 30 counter ++ ; 31 is 32 IF (% counter ( 15 ) == 0 ) { 33 is // do something 34 is } 35 36 } 37 38 39 @end
Reproduced in: https: //www.cnblogs.com/pretty-guy/p/4067495.html