http://blog.csdn.net/wzzvictory/article/details/22417181
Core Animation series Cadisplaylinkwangzz Original address: http://blog.csdn.net/wzzvictory/article/details/ 22417181 Reprint Please specify the source if you feel that the article is helpful, please leave a message or follow Weibo, public account wangzzstrive to support me, thank you! Have always wanted to learn the next coreanimation, but the things involved too much, want to take all the time not allowed, will be intermittent completion. Using Cadisplaylink in the recent project, I took some time to look at it. I. INTRODUCTION 1, the framework Cadisplaylink and other Coreanimation class, are in the quartzcore.framework. 2, the main feature of functional cadisplaylink is to provide a periodic call to the selector we assign it to the mechanism, from this point of view it is like a timer nstimer. 3. How to use
[OBJC]View Plaincopy
- -(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 is add to the Runloop, selector can be called periodically, similar to Nstimer being started, and invalidate objects are removed from Cadisplaylink when the Runloop operation is performed. The selector call is also stopped, similar to the Nstimer invalidate method. Second, the characteristics of the following combined with Nstimer to introduce Cadisplaylink, and Nstimer different places are: 1, the principle of different cadisplaylink is a can let us in and screen refresh rate synchronization of the frequency of the specific content to draw to the screen timer class. Once the Cadisplaylink is registered to Runloop in a specific mode, Runloop sends a specified selector message to the target specified by Cadisplaylink whenever the screen displays the end of the content refresh, The corresponding selector of the Cadisplaylink class is called once. After the Nstimer is registered with the Runloop in the specified mode, Runloop sends a specified selector message to the specified target whenever the set cycle time arrives. 2, cycle settings different iOS device screen refresh frequency (FPS) is 60Hz, so cadisplaylink selector default call cycle is 60 times per second, this cycle can be set through the Frameinterval property, The number of Cadisplaylink selector calls per second =60/frameinterval. For example, when Frameinterval is set to 2, the call becomes 30 times per second. Therefore, the setting of the Cadisplaylink cycle is slightly inconvenient. The selector call cycle of Nstimer can be set directly at initialization, which is more flexible. 3, the accuracy of different iOS device screen refresh frequency is fixed, cadisplaylink under normal circumstances will be called at the end of each refresh, the accuracy is quite high. The accuracy of the nstimer appears to be low, such as when the Nstimer trigger time is up, runloop if it is busy with another call, the trigger time is postponed to the next Runloop cycle. What's more, after OS X v10.9 in order to try to avoid the Nstimer trigger time to interrupt the current processing of the task, Nstimer added tolerance attribute, allowing the user to set the acceptable trigger time range. 4, the use of the occasion from the principle is not difficult to see, cadisplaylink Use the occasion is relatively exclusive, suitable for the interface of the constant repainting, such as video playback need to constantly getRemove a frame for interface rendering. Nstimer can be used in a wide range of applications, with a variety of tasks that require a single-or cyclic-timing process. Important Properties The following is an incomplete list of several important properties of Cadisplaylink: 1, frameinterval readable writable Nsinteger value, the identity interval how many frames call selector method, the default value is 1, that is, each frame is called once. The official document emphasizes that when the value is set to less than 1 o'clock, the result is unpredictable. 2. Duration read-only cftimeinterval value, which indicates the time interval between two screen refreshes. It is important to note that this property will not be assigned until the target's selector is first called. The selector call interval is calculated by: Time =durationxframeinterval. The existing iOS device screen has a 60Hz fps, which can be seen from the Cadisplaylink duration attribute. The value of duration is 0.166666 ..., which is 1/60. However, we are not sure that Apple will not change FPS, if the next day to increase the FPS to 120Hz, how to do? At this point, you set the value of the Frameinterval property to 2, 30 times per second, but found that refreshed 60 times per second, the results can be imagined, for security reasons, or first according to duration to determine the screen FPS to use Cadisplaylink. 3. Timestamp read-only Cftimeinterval value that represents the timestamp of the previous frame displayed on the screen, which is typically used by target to calculate what should be displayed in the next frame.
Prints the timestamp value with a style similar to:
[OBJC]View Plaincopy
- 179699. 631584
Although known as timestamps, this differs greatly from the common Unix timestamp, which is in fact the time format used by coreanimation. Each calayer has a local time (the specific role of Calayer local time is described in a subsequent article), and you can obtain the local time for the current Calayer and print:
[OBJC]View Plaincopy < param name= "wmode" value= "Transparent" >
- Cftimeinterval locallayertime = [Mylayer converttime:cacurrentmediatime () fromlayer: nil];
- NSLog ("locallayertime:%f", locallayertime);
Iv. Note that iOS does not guarantee that the callback method can be called at a frequency of 60 times per second, depending on:
1, CPU idle degree if the CPU is busy with other computations, there is no guarantee that the drawing action of the screen will be executed at 60HZ, causing the chance to skip several calls to the callback method, depending on how busy the CPU is.
2. The time taken to execute the callback method, if the execution callback time is greater than the interval between redrawing of each frame, causes a number of callback invocation opportunities to be skipped, depending on the length of execution. V. Reference Document 1, Official document https://developer.apple.com/library/ios/documentation/QuartzCore/Reference/CADisplayLink_ClassRef/ Reference/reference.html#//apple_ref/doc/uid/tp40009031-ch1-dontlinkelementid_12, official use Cadisplaylink to play video example HTTPS ://developer.apple.com/library/ios/samplecode/avbasicvideooutput/introduction/intro.html#//apple_ref/doc/uid/ DTS40013109
Core Animation series Cadisplaylink