Use of iOS timers

Source: Internet
Author: User
Tags gcd

Timers are often used in iOS development, and there are three types of timers commonly used in iOS, Nstime,cadisplaylink and GCD, respectively.

Nstimer

Mode 1

//Create timer

nstimer *Timer = [nstimer scheduledtimerwithtimeinterval:2 Target:self selector:@selector(test) userInfo:Nil Repeats:YES];


//Stop timer

[timer invalidate];


Mode 2

//Create timer

Nstimer*Timer =[nstimer timerwithtimeinterval:2 target:self selector:@selector(test) userInfo:nil repeats:YES ];

//Add timer to Runloop, otherwise the timer will not start

[[nsrunloop mainrunloop] addtimer:timer formode:nsrunloopcommonmodes];


//Stop timer

[timer invalidate];

Mode 1 automatically adds the created timer to the current thread runloop by default, without adding it manually. However, in this mode, when scrolling the screen runloop will enter another mode, the timer will be paused, in order to solve this problem, you can add the timer as in mode 2 to Nsrunloopcommonmodes mode.

Mode 1 and Mode 2 will be set after the interval set at the time (set to 2s in this example) after the test method, if necessary to execute immediately can use the following code.

[Time Fire];

Cadisplaylink

//Create DisplayLink

cadisplaylink*displayLink =[cadisplaylink displaylinkwithtarget:Self selector:@selector(test:)];

//Add the created DisplayLink to the Runloop, or the timer will not execute

[displayLink addtorunloop: [nsrunloop mainrunloop] formode: Nsdefaultrunloopmode];


//Stop timer

[displayLink invalidate];

DisplayLink =nil;

When the Cadisplaylink object is add to the Runloop, selector can be called periodically, similar to duplicate Nstimer is initiated, invalidate objects are removed from Cadisplaylink when the runloop operation is performed , the selector call also stops, similar to the Nstimer invalidate method

Call timing

Cadisplaylink is a timer class that synchronizes 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, so you can use Cadisplaylink to do something related to the screen operation.

Important attributes

    • Frameinterval
      The value of the Nsinteger type, used to set the interval for how many frames are called once selector method, the default value is 1, which is called once per frame.
    • Duration
      The Cftimeinterval value of ReadOnly, which represents the 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 call interval for selector is calculated by calling interval time = Durationxframeinterval.

GCD Timer

Disposable timing

dispatch_time_t Timer= dispatch_time(dispatch_time_now,1.0 * Nsec_per_sec);


Dispatch_after(timer, dispatch_get_main_queue(),^ (void) {


NSLog(@"GCD-----%@", [nsthread CurrentThread]);


});


Recurring timer

@property (nonatomic,strong)dispatch_source_t timer; Note: Strong references should be used here strong

{

//0. Creating a queue

dispatch_queue_t Queue= dispatch_get_main_queue();

//1. Creating timers in the GCD

/*

First parameter: Create the type of SOURCE Dispatch_source_type_timer: Timer

Second parameter: 0

Third parameter: 0

Fourth parameter: queue

*/

dispatch_source_t Timer= dispatch_source_create(dispatch_source_type_timer,0 , 0, queue);


//2. Setting time, etc.

/*

First parameter: Timer object

Second parameter: Dispatch_time_now means timing from now on

Third parameter: Interval time GCD the smallest unit of time in nanoseconds

Fourth parameter: accuracy (indicates allowable error, 0 means absolute accuracy)

*/

Dispatch_source_set_timer(timer, dispatch_time_now,1.0 * nsec_per_sec, 0 * nsec_per_sec);


//3. The task to invoke

Dispatch_source_set_event_handler(timer,^{

NSLog(@"GCD-----%@", [nsthread CurrentThread]);

});


//4. Start Execution

Dispatch_resume(timer);


//

self. Timer= timer;

}

Note Here must be stronger reference timer , otherwise the timer will be released after execution, no timing effect.

Use of iOS timers

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.