IOS RunLoop 1. iosrunloop
A RunLoop is like a loop of its name.
RunLoop has multiple modes.
You can only execute one mode at a time.
Therefore, when using RunLoop, pay attention that the effect may not be what you want.
Here we use NSTimer to demonstrate the simple implementation of Runloop.
Add a TextView to the story board for testing)
Let's add nstmode to NSDefaultRunLoopMode.
We can clearly see that nstimer is not executed when we scroll TextView.
/// ViewController. m // CX RunLoop analysis // Created by ma c on 16/3/29. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad];}-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {NSTimer * timer = [NSTimer timerWithTimeInterval: 3 target: self selector: @ selector (test) userInfo: nil repeats: YES]; // Add it to the default runloop [[[nsunloop mainRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode]; [timer fire];}-(void) test {NSLog (@ "xubao loves fish");} @ end
Let's add nstmode to UITrackingRunLoopMode.
We can clearly see that nstimer is executed when we scroll TextView.
/// ViewController. m // CX RunLoop analysis // Created by ma c on 16/3/29. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad];}-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {NSTimer * timer = [NSTimer timerWithTimeInterval: 3 target: self selector: @ selector (test) userInfo: nil repeats: YES]; // Add it to the default runloop [[nsunloop currentRunLoop] addTimer: timer forMode: UITrackingRunLoopMode]; [timer fire];}-(void) test {NSLog (@ "xubao loves fish");} @ end
Let's add nstdes to the nsunloopcommonmodes mode.
We can clearly see that nstimer is executed when we scroll or do not scroll TextView.
/// ViewController. m // CX RunLoop analysis // Created by ma c on 16/3/29. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad];}-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {NSTimer * timer = [NSTimer timerWithTimeInterval: 3 target: self selector: @ selector (test) userInfo: nil repeats: YES]; // Add it to the default runloop [[nsunloop currentRunLoop] addTimer: timer forMode: nsunloopcommonmodes]; [timer fire];}-(void) test {NSLog (@ "xubao loves fish");} @ end
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval: 2 target: self selector: @ selector (test) userInfo: nil repeats: YES];
Automatically added to runloop and the default value is NSDefaultRunLoopMode.
However, we can change the mode in the same way as above.
/// ViewController. m // CX RunLoop analysis // Created by ma c on 16/3/29. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad];}-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval: 2 target: self selector: @ selector (test) userInfo: nil repeats: YES];}-(void) test {NSLog (@ "xubao loves fish");} @ end