iOS series--nstimer

Source: Internet
Author: User

Some of the things that the timer often uses

1. Initialization

Timer = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (changetime:) Userinfo:nil <span Style= "Font-family:arial, Helvetica, Sans-serif;" > repeats:yes];</span>

2.timer Run Now

[Tiemr fire];
Assuming that this code is not added at initialization time, the timer will run back immediately.


3. Timer Expiration

[Timer invalidate];<pre name= "code" class= "html" >timer = nil;    When the timer fails, it's best to empty it.
After this failure is not another time to use the timer, it is equivalent to a timer useless. To continue using a timer can only initialize the timer again


4.timer Pause

[Timer setfiredate:[nsdate distantfuture];


5.timer pause and start again

[Timer setfiredate:[nsdate Distantpast];


6.timer Method Transfer

The number of passes is an ID type, and we generally put all the passes in the nsdictionary.

    Nsdictionary *dic = [nsdictionary dictionarywithobject:[nsstring stringwithformat:@ "%i", AnimIndexPath] forKey:@ " Animindexpath "];    Timer = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (changetime:) Userinfo:dic repeats: YES];


In the method that the timer runs
-(void) Changetime: (Nstimer *) tme{        int soundlength = [[Tme userInfo] objectforkey:@ "Animindexpath"] intvalue];    }}
So we can get the number of the passes.



7. Infer if the timer is executing

if ([timer isValid]) {        [timer invalidate];        Timer = nil;    }


iOS series--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.