Seven, the difficulty of the timer

Source: Internet
Author: User

In the actual development, we will encounter a scene is: With + scheduledtimerwithtimeinterval ... The way the timer object, which acts on the list on the Uiscrollview, has a tentative callback and is very mad at the failure to understand its rationale. After viewing the official documents and the diary of the Great God, I finally figured it out.

There are two ways to create a timer >: +scheduledtimerwithtimeinterval, +timerwithtimeinterval.

The first method of creating a timer (in most cases using this method)

Scheduledtimerwithtimeinterval: The essence of doing two things:

> Created a Timer object

> Registers the timer listener method with the Runloop (running loop) and adds it in the default Nsdefaultrunloopmode mode, which causes the timer to pause when the list is dragged on Uiscrollview.

Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (nextimage) Userinfo:nil Repeats:yes];

You need to manually add the timer to the run cycle in nsrunloopcommonmodes mode, and if the timer does not fail, it will continue to run.

[[Nsrunloop Currentrunloop] AddTimer:self.timer formode:nsrunloopcommonmodes];

The second method of creating a timer

Timerwithtimeinterval: The Timer object created by this method is not added to the run loop by default and needs to be manually registered to Runloop (running loop).

Nstimer *timer = [Nstimer timerwithtimeinterval:1.0 target:self selector: @selector (nextimage) Userinfo:nil Repeats:YES ];

The timer is automatically turned on as long as the timer object is added to the run loop

[[Nsrunloop Currentrunloop] AddTimer:self.timer formode:nsrunloopcommonmodes];

This method only triggers the callback method once.

[Timer fire];

> When the timer is not needed, remember to remove the timer

Disable the timer: Once the timer expires, it can no longer be used, only to recreate a new timer

[Timer invalidate];

Timer = nil;

Seven, the difficulty of the timer

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.