For a long time, I have always wanted to learn coreanimation, but there are too many things involved. If I want to finish it all at once, I won't allow it. In the future, I will make up for it intermittently. Cadisplaylink was used in the recent project, so I took some time to look at it. I. Introduction 1. The cadisplaylink framework is the same as other coreanimation classes and is in quartzcore. framework. 2. The main feature of the function cadisplaylink is to provide a mechanism for periodically calling the selector we assign to it. From this point of view, it is similar to the timer nstimer. 3. Usage
[Objc]View plaincopy
- -(Void) startdisplaylink
- {
- Self. displaylink = [cadisplaylink displaylinkwithtarget: Self
- Selector: @ selector (handledisplaylink :)];
- [Self. displaylink addtorunloop: [nsunloop currentrunloop]
- Formode: nsdefaultrunloopmode];
- }
- -(Void) handledisplaylink :( cadisplaylink *) displaylink
- {
- // Do something
- }
- -(Void) stopdisplaylink
- {
- [Self. displaylink invalidate];
- Self. displaylink = nil;
- }
After the cadisplaylink object is added to runloop, the selector can be called cyclically, similar to nstimer being started. When the invalidate operation is executed, the cadisplaylink object will be removed from runloop, the selector call is stopped immediately, similar to the invalidate method of nstimer. Ii. features the following describes cadisplaylink with nstlink, which is different from nstimer: 1. Different principles cadisplaylink is a Timer class that allows us to draw specific content to the screen at a frequency that synchronizes with the screen update rate. After 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 content refreshed, the selector corresponding to the cadisplaylink class will be called once. After nsttor registers to runloop in the specified mode, runloop sends a specified selector message to the specified target whenever the set cycle time arrives. 2. The screen refresh frequency (FPS) of different iOS devices is 60Hz. Therefore, the selector of cadisplaylink is called 60 times per second by default. This period can be set through the frameinterval attribute, the number of times that the selector of cadisplaylink calls per second = 60/frameinterval. For example, if frameinterval is set to 2, the number of calls per second is changed to 30. Therefore, it is inconvenient to set the cadisplaylink period. The selector call cycle of nstimer can be set directly during initialization, which is much more flexible. 3. The screen refresh frequency of different iOS devices is fixed. cadisplaylink is normally called at the end of each refresh, with a high accuracy. The accuracy of nstimer is low. For example, when the nstimer trigger time is reached, if runloop is busy with other calls, the trigger time will be postponed to the next runloop cycle. What's more, after OS X v10.9, nstance adds the tolerance attribute in order to avoid interrupting the current processing task when the nstimer trigger time is reached, allows you to set a tolerable time range for triggering. 4. usage scenarios it is not difficult to see from the principle that cadisplaylink is relatively specific in usage scenarios and is suitable for non-stop re-painting of the interface. For example, you need to continuously obtain the next frame during video playback for interface rendering. Nstimer is widely used, and can be used for various tasks that require single or cyclic scheduled processing. 3. The important attributes below list the important attributes of cadisplaylink in an incomplete way: 1. frameinterval readable and writable nsinteger value, which indicates the number of frames at which the selector method is called. The default value is 1, that is, each frame is called once. The official document emphasizes that when this value is set to less than 1, the results are unpredictable. 2. The cftimeinterval value of duration read-only indicates the interval between two screen refreshes. Note that this attribute is assigned a value only after the target selector is called for the first time. The call interval Calculation Method of selector is: time = duration × frameinterval. The FPs of the existing iOS device is 60Hz, which can be seen from the Duration Attribute of cadisplaylink. Duration values are all 0.166666 ..., 1/60. However, we cannot determine that Apple will not change the FPS. What if we increase the FPS to Hz one day? At this time, you set the frameinterval attribute value to 2 and expect to refresh 30 times per second, but you find that the refresh is 60 times per second. The result is conceivable. For security considerations, determine the FPs of the Screen Based on duration and then use cadisplaylink. 3. The cftimeinterval value of Timestamp read-only indicates the timestamp of the last frame displayed on the screen. This attribute is usually used by target to calculate the content to be displayed in the next frame.
Print the timestamp value. The style is similar:
[Objc]View plaincopy
- 179699.631584
Although the name is a timestamp, it is very different from the common UNIX timestamp. In fact, this is the time format used by coreanimation. Each calayer has a local time (the specific role of calayer local time will be described in subsequent articles). You can get the local time of the Current calayer and print it:
[Objc]View plaincopy
- Cftimeinterval locallayertime = [mylayer converttime: cacurrentmediatime () fromlayer: Nil];
- Nslog ("locallayertime: % F", locallayertime );
4. Note that IOS does not guarantee that the callback method can be called 60 times per second, depending on:
1. CPU idleness if the CPU is busy with other calculations, it cannot be guaranteed to execute screen rendering at 60Hz, leading to skipping several chances to call the callback method, the number of skipping times depends on how busy the CPU is.
2. The time used to execute the callback method. If the execution callback time is greater than the interval of re-painting each frame, several callback call opportunities will be skipped, depending on the execution duration. V. Reference Document 1. Official documents refer
Cadisplaylink of core animation series