IOS timer Three kinds of timer usage nstimer, Cadisplaylink, GCD

Source: Internet
Author: User
Tags gcd

Original: http://www.cocoachina.com/ios/20160919/17595.html

One or three types of timers

Second, the Global countdown

#import "ViewController.h"@interfaceViewcontroller () {Cadisplaylink*displaylinked;}@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib. //1 timer will be delayed enough to be accurate[self tiemred]; //2. Cadisplaylink for UI Refresh animations[self cadisplaylink]; //3 GCD High accuracy[self gcdtimer]; }-(void) gcdtimer{//GCD timers and Nstimer are not the same, Nstimer affected by Runloop, but the GCD timer is not affected, because Runloop is also based on GCD//1 performed 1 times    DoubleDelayinseconds =2.0;//time intervaldispatch_time_t Poptimer= Dispatch_time (Dispatch_time_now, Delayinseconds *nsec_per_sec); Dispatch_after (Poptimer, Dispatch_get_main_queue (),^{        //Execution Events            }); //repeated executionNstimeinterval period =1.0;//time intervaldispatch_queue_t Queuet = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); dispatch_source_t Timer= Dispatch_source_create (Dispatch_source_type_timer,0,0, Queuet); Dispatch_source_set_timer (timer, Dispatch_time_now, period* Nsec_per_sec,0*nsec_per_sec); Dispatch_source_set_event_handler (Timer,^{        //Execution Events                    });    Dispatch_resume (timer); }-(void) cadisplaylink{//Createdisplaylinked =[Cadisplaylink displaylinkwithtarget:self selector: @selector (displayaction:)]; //Join Runloop[displaylinked addtorunloop:[nsrunloop Currentrunloop] formode:nsdefaultrunloopmode]; /*Cadisplaylink is a timer class that allows us to draw specific content to the screen at a frequency that is synchronized with the screen refresh rate. 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. The screen refresh rate for iOS devices is fixed, and cadisplaylink is normally called at the end of each flush, with very high accuracy. 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. Do not need to be particularly concerned about the screen refresh frequency, itself is synchronized with the screen refresh*/        /*What's the difference between Cadisplaylink and nstimer? The screen refresh frequency of iOS devices is fixed, and cadisplaylink is normally called at the end of each flush, with very high accuracy. The accuracy of the nstimer is low, such as when the Nstimer trigger time is up, runloop if in the blocking state, the trigger time is postponed to the next Runloop cycle.     And Nstimer has added the tolerance attribute, which allows the user to set the delay range of the time that can be tolerated for triggering. Cadisplaylink is a relatively exclusive use case, suitable for the constant redrawing of the UI, such as a custom animation engine or video playback rendering. Nstimer can be used in a wide range of applications, with a variety of tasks that require a single-or cyclic-timing process.     The benefit of UI-related animations or display content using Cadisplaylink than Nstimer is that we don't need to be particularly concerned about the refresh rate of the screen because it's in itself synchronized with the screen refresh. The following combination of Nstimer to introduce Cadisplaylink, and Nstimer different places are: 1, the principle of different cadisplaylink is a can let us with the 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 continuous repainting of the interface, such as video playback need to constantly get the next frame for the 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.*/}-(void) Displayaction: (cadisplaylink*) display{NSLog (@"not a single frame is being refreshed"); //Display's cancellationdisplay.paused =YES;    [Display invalidate]; Display=Nil; }-(void) tiemred{//Timer Timers//1 No loopNstimer * Timerno = [Nstimer scheduledtimerwithtimeinterval:3target:self selector: @selector (timeraction:) Userinfo:nil Repeats:no]; //Loop TimerNstimer * Timeryes = [Nstimer scheduledtimerwithtimeinterval:3target:self selector: @selector (timeraction:) Userinfo:nil Repeats:yes]; //join Runloop to ensure that runloop change is the timer works[[Nsrunloop Currentrunloop] Addtimer:timeryes formode:nsrunloopcommonmodes]; }-(void) Timeraction: (nstimer*) timer{NSLog (@"Timer's coming."); //Timer Destruction//[timer invalidate];//timer = nil;}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end

IOS timer Three kinds of timer usage nstimer, Cadisplaylink, GCD

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.