Core Animation series CADisplayLink

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
Core Animation series CADisplayLink
Author: wangzz
The original address:
Reprint please indicate the source
If you feel the article has the help to you, please support me through the message or attention micro-blog, Micro message public account wangzzstrive, thank you!


I want to have been Study hard under the CoreAnimation, it involves too many things all at once, want to get is not allowed on time, off and later completion. Recent projects in the use of CADisplayLink, will take time to look at.

One, introduction

1, The framework

CADisplayLink and other CoreAnimation class, are in the QuartzCore.framework.

2, Function

The main characteristics of CADisplayLink is to provide a periodic calls us to assign it to the selector mechanism, from this point of view it is very much like the timer NSTimer.

3, Using way

- (void)startDisplayLink
{
    self.displayLink = [CADisplayLink displayLinkWithTarget:self
                                                   selector:@selector(handleDisplayLink:)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop]
                           forMode:NSDefaultRunLoopMode];
}

- (void)handleDisplayLink:(CADisplayLink *)displayLink
{
  //do something
}

- (void)stopDisplayLink
{
    [self.displayLink invalidate];
    self.displayLink = nil;
}

When the CADisplayLink object add to runloop in, Selector can be called periodically, Similar to the NSTimer is activated when performing the invalidate operation, The CADisplayLink object will be removed from the runloop, The selector call is immediately stop, The invalidate method is similar to NSTimer.


Two, characteristics

The binding of NSTimer to CADisplayLink, and NSTimer in different places:

1, Different principles

CADisplayLink is a can let us and the screen refresh rate synchronous frequency specific content to the screen timer class. CADisplayLink in a specific mode is registered to the runloop, when the screen display refresh end, runloop will send selector message CADisplayLink the specified target a specified, the CADisplayLink class corresponding to the selector will be called a.

NSTimer with the specified pattern is registered to the runloop, when the cycle time arrives, runloop will provide selector message to send the target a specified.

2, Cycle settings in different ways

IOS device screen refresh rate (FPS) is 60Hz, So the CADisplayLink selector default call cycle is 60 times per second, This cycle can be obtained by setting the frameInterval property, The number of selector calls per second =60 CADisplayLink/frameInterval. For example, when the frameInterval is set to 2, the call will become 30 times per second. Therefore, CADisplayLink cycle is set slightly inconvenient.
NSTimer selector call cycle can be set directly in the initialization, relatively more flexible.

3, Different accuracy

IOS device screen refresh rate is fixed, the CADisplayLink every time you refresh the end is called under normal circumstances, precision is high.
The accuracy of NSTimer is lower, such as the NSTimer trigger time to time, if runloop is busy with other calls, triggering time will be postponed to the next runloop cycle. What is more, in the OS X v10.9 in order to avoid as far as possible in the NSTimer trigger time arrived to interrupt the current processing tasks, NSTimer adds the tolerance attribute, so that users can set can tolerate the trigger time range.

4, The use of occasions

The principle is not difficult to see, CADisplayLink uses the relative specific occasions, don't stop to redraw the suitable interface, such as video playback needs constantly to obtain the next frame for interface rendering.
The use of NSTimer range is more extensive than the various needs, single or cyclic timing processing tasks can be used.


Three, an important attribute


The following incomplete lists some important properties of CADisplayLink:

1, frameInterval

Read / write the NSInteger value identifies how many frames, interval selector method is invoked once, the default value is 1, which are called once per frame. Stressed that the official documents, when the value is set to less than 1, the result is unpredictable.

2, duration

Read only CFTimeInterval value, say two times the screen refresh interval between. Of note, the target attribute in the selector was the first call before being assigned. Call the interval selector is calculated by: =duration×frameInterval.

The existing iOS equipment screen of FPS is 60Hz, which can be seen from the duration property of CADisplayLink. The value of duration is 0.166666... 1, i.e./60. In spite of this, We are not sure Apple does not change FPS, If a later day raised FPS to do 120Hz? Then, You set the frameInterval property value is 2 per second refresh 30 times expected, They found 60 times per second refresh, Results one can imagine, For security reasons, Or to judge by duration screen FPS to use CADisplayLink.

3, timestamp

Read only CFTimeInterval value, said screen displays a frame time stamp, this property is often used by target to calculate should be shown in the next frame content.
Print timestamp value, its style is similar to the:
179699.631584
Although the name is time stamp, but this and the common UNIX time stamp difference is very big, in fact this is the use of CoreAnimation time format. Each CALayer has a local time (the specific role of CALayer local time will be explained in the next paper), can obtain the current local time in CALayer and print:
CFTimeInterval localLayerTime = [myLayer convertTime:CACurrentMediaTime() fromLayer:nil];
NSLog("localLayerTime:%f",localLayerTime);




Four, pay attention to

IOS does not guarantee to invoke the callback method in the frequency of 60 times per second, depending on the:

1, Free CPU

If CPU is busy with other computation, can not guarantee to 60HZ execution screen rendering action, leading to skip some calls the callback method opportunity, skip the number depends CPU how busy the.

2, The callback method of time

If the time interval is greater than the redraw callback time per frame, will lead to skip callback calls several times, depending on the length of execution time.



Five, reference documentation

1, The official document

https://developer.apple.com/library/ios/documentation/QuartzCore/Reference/CADisplayLink_ClassRef/Reference/Reference.html#//apple_ref/doc/uid/TP40009031-CH1-DontLinkElementID_1

2, The official video using the example of CADisplayLink

https://developer.apple.com/library/ios/samplecode/AVBasicVideoOutput/Introduction/Intro.html#//apple_ref/doc/uid/DTS40013109
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Robert at May 03, 2014 - 5:25 AM