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