CADisplayLink for iOS Core Animation series

Source: Internet
Author: User

CADisplayLink for iOS Core Animation series
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

  1. -(Void) startDisplayLink
  2. {
  3. Self. displayLink = [CADisplayLinkdisplayLinkWithTarget: self
  4. Selector: @ selector (handleDisplayLink :)];
  5. [Self. displayLinkaddToRunLoop: [nsunloopcurrentrunloop]
  6. ForMode: NSDefaultRunLoopMode];
  7. }
  8.  
  9. -(Void) handleDisplayLink :( CADisplayLink *) displayLink
  10. {
  11. // Dosomething
  12. }
  13.  
  14. -(Void) stopDisplayLink
  15. {
  16. [Self. displayLinkinvalidate];
  17. Self. displayLink = nil;
  18. } After the CADisplayLink object is added to runloop, the selector can be called cyclically, similar to NSTimer 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
     
    1. 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
      1. CFTimeIntervallocalLayerTime = [myLayerconvertTime: CACurrentMediaTime () fromLayer: nil];
      2. NSLog ("localLayerTime: % f", localLayerTime); 4. Note that iOS cannot ensure 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.