An analysis of IOS Runloop

Source: Internet
Author: User

Runloop is like a circle of its name.

There are multiple modes in the Runloop.

In a "moment" only the value executes a pattern.

So when using Runloop, be aware that the effect you have achieved may not be what you want.

Here, use Nstimer to show the simple implementation of Runloop.

Add a TextView (for testing) in the storyboard

Let's go, Nstimer. Add to Nsdefaultrunloopmode mode

In the above we can clearly see that when we roll TextView, Nstimer is not executing.

////VIEWCONTROLLER.M//A brief analysis of CX Runloop////Created by Ma C on 16/3/29.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload];}-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{Nstimer* Timer = [Nstimer timerwithtimeinterval:3target:self selector: @selector (test) Userinfo:nil Repeats:yes]; //Add to default Runloop[[Nsrunloop Mainrunloop]addtimer:timer Formode:nsdefaultrunloopmode];                       [Timer fire];} -(void) test{NSLog (@"Xu Bao love to eat fish"); }@end

Let's go, Nstimer. Add to Uitrackingrunloopmode mode

In the above we can see very clearly, when we roll TextView, Nstimer executes.

////VIEWCONTROLLER.M//A brief analysis of CX Runloop////Created by Ma C on 16/3/29.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload];}-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{Nstimer* Timer = [Nstimer timerwithtimeinterval:3target:self selector: @selector (test) Userinfo:nil Repeats:yes]; //Add to default Runloop[[Nsrunloop Currentrunloop]addtimer:timer Formode:uitrackingrunloopmode];                       [Timer fire];} -(void) test{NSLog (@"Xu Bao love to eat fish"); }@end

Let's go, Nstimer. Add to Nsrunloopcommonmodes mode

In the above we can see very clearly, when we scroll and do not scroll textview, Nstimer are executed.

////VIEWCONTROLLER.M//A brief analysis of CX Runloop////Created by Ma C on 16/3/29.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload];}-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{Nstimer* Timer = [Nstimer timerwithtimeinterval:3target:self selector: @selector (test) Userinfo:nil Repeats:yes]; //Add to default Runloop[[Nsrunloop Currentrunloop]addtimer:timer formode:nsrunloopcommonmodes];                       [Timer fire];} -(void) test{NSLog (@"Xu Bao love to eat fish"); }@end

Nstimer * timer = [Nstimer scheduledtimerwithtimeinterval:2 target:self selector: @selector (Test) Userinfo:nil repeats: YES];

Automatically added to Runloop and defaults to Nsdefaultrunloopmode.

But we can change the pattern by the same method as above.

////VIEWCONTROLLER.M//A brief analysis of CX Runloop////Created by Ma C on 16/3/29.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload];}-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{Nstimer* Timer = [Nstimer scheduledtimerwithtimeinterval:2target:self selector: @selector (test) Userinfo:nil Repeats:yes];} -(void) test{NSLog (@"Xu Bao love to eat fish"); }@end

An analysis of IOS Runloop

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.