"Go" IOS timer Nstimer

Source: Internet
Author: User

Original URL: http://blog.csdn.net/tangshoulin/article/details/7644124

1. Initialization

+ (Nstimer *) Timerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector userInfo: (ID) UserInfo repeats: (BOOL) Yesorno;

+ (Nstimer *) Scheduledtimerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector UserInfo :(ID) UserInfo repeats: (BOOL) Yesorno;

Note: Without scheduled initialization, manual Addtimer:formode is required: Add the timer to a runloop.

The scheduled initialization method will be added directly to the current runloop with the default mode.

Example:

Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:10.0 target:self selector: @selector (timerfired:) UserInfo: Nil Repeats:no];

Or

Nstimer *MyTimer = [nstimertimerwithtimeinterval:3.0 target:selfselector:@ Selector(timerfired:) userInfo:nilrepeats:NO];

[[nsrunloopcurrentrunloop] addtimer:mytimerformode:nsdefaultrunloopmode];

2. Trigger (START)

When the timer is created (not scheduled, added to Runloop, the timer is automatically triggered after the timeinterval seconds specified at initialization.

You can use the-(void) Fire method to trigger the timer immediately;

Note: You can use the This method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it's automatically invalidated after firing, even if its scheduled fire date have not Arriv Ed.

The timer is triggered immediately after calling this method in a repeating timer, but does not interrupt its previous execution plan;

Calling this method in a timer that does not execute repeatedly will invalidate the timer immediately after it is triggered.

3. Stop

-(void) invalidate;

This is the only way to move the timer out of the Runloop.

Note:

The Nstimer can be accurate to 50-100 milliseconds.

The Nstimer is not absolutely accurate, and the intermediate time-consuming or blocking misses the next point, then the next point passes.

Use code Examples:

The timer that works well in the game Loop-(void) starttimer{//repeats is set to Yes when re-executing after each invalidate, and if no, the timer is not valid after the logic executes [nstimer  scheuledtimerwithtimeinterval:1.0/60.0 Target:view selector:@ (timeradvanced:) Userinfo:nil repeats:yes];}  -(void) timeradvanced: (Nstimer *) timer//This function will execute a loop of logic {//plus a counter int mTime;    if (MTime = = 0) {[timer invalidate];  [Self starttimer];  } mtime++;    if (mTime >= a maximum value maxcount)//Handle some logic ..... Reset Mtime to 0} somewhere

"Go" IOS timer Nstimer

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.