What is Cadisplaylink
CADisplayLink
is a timer that allows us to draw content to the screen at the same frequency as the screen refresh rate. We create a new object in the app CADisplayLink
, add it to the one runloop
, and give it one target
and call it selector
when the screen refreshes.
One but cadisplaylink
Register to Runloop
in a specific mode, runloop
when the screen needs to be refreshed selector
on the target
of the cadisplaylink
binding is called, at which point target
can read the The timestamp of each invocation of Cadisplaylink
is used to prepare the next frame to display the required data. For example, a video application uses timestamps to calculate the video data to be displayed for the next frame. In the process of animating the UI, the time stamp is needed to calculate the size of the UI object to be updated in the next frame of the animation, and so on.
When adding runloop
, we should use a higher priority to ensure the smooth animation. As you can imagine, in the process of animation, when Runloop
is added to a high-priority task, the next call is paused to perform a high-priority task first, then executes the cadisplaylink The
call, which causes the animation process to stutter, making the animation not smooth.
duration
Property provides the time between each frame, that is, the time between each refresh of the screen. We can use this time to calculate the value of the UI to be displayed for the next frame. But it's duration
just a rough time, and if the CPU is busy with other calculations, there's no guarantee that the screen's drawing will be performed at the same frequency, skipping the chance to invoke the callback method several times.
frameInterval
A property is a readable, writable NSInteger
type value that identifies how many frames are called once, and the selector
default value is 1, which is called once per frame. If each frame is called once, the refresh rate for IOS devices is 60HZ, which is 60 times per second, if frameInterval
set to 2 then two frames will be called once, that is, the refresh 30 times per second.
We pause
run through the property CADisplayLink
-open control. When we want to end one CADisplayLink
, we should call-(void)invalidate
runloop
Remove from and delete the previously bound target
selector
In addition CADisplayLink
cannot be inherited.
CADisplayLink
And
NSTimer
What's the difference?
The screen refresh frequency of the IOS device is fixed and is CADisplayLink
normally called at the end of each flush, with very high accuracy.
NSTimer
The accuracy is low, such as when the NSTimer
trigger time, runloop
if in the blocking state, the trigger time will be postponed to the next runloop
cycle. And NSTimer
added tolerance
properties that allow the user to set the delay range for the time that can be tolerated.
CADisplayLink
The use of a relatively exclusive, suitable for the UI of the constant repainting, such as custom animation engine or video playback of the rendering. NSTimer
use a wide range of applications, a variety of tasks that require a single or cyclic timing processing can be used. The advantage of using the UI-related animations or display content CADisplayLink
NSTimer
is that we don't need to be particularly concerned about the refresh rate of the screen, because it's in sync with the screen refresh itself.
Examples of Cadisplaylink use
Self.displaylink == yes;[ Self.displaylink Addtorunloop:[nsrunloop Currentrunloop] formode:nsrunloopcommonmodes]; -(void) updatetextcolor{}-(void) startanimation{ = cacurrentmediatime (); = NO;} -(void) stopanimation{ = YES; [Self.displaylink invalidate]; = Nil;}
Add animations to non-UI objects
We know that the animation effect is a linear variation of a property, such as the easyineasyout of a uiview animation. We can produce animations that are closer to the real world by varying the rate of values. We can also use this feature to make some other properties change according to the curve we expect. For example, when the video is played off the sound of the video I can CADisplayLink
achieve a easyout effect: the first to quickly lower the volume, slowly fade to mute.
Attention
Generally speaking: The refresh frequency of iOS devices is 60HZ 60 times per second. Then each refresh time is 1/60 seconds, about 16.7 milliseconds. When we frameInterval
have a value of 1 , we need to ensure that the function of the CADisplayLink
call ' target ' should not be greater than 16.7, otherwise there will be a serious drop frame phenomenon.
In the Mac application we are not using CADisplayLink
but CVDisplayLink
it is based on the C interface of the use of the configuration some trouble but it is very simple to use.
The difference between Cadisplaylink and Nstimer