IOS RunLoop 1. iosrunloop

Source: Internet
Author: User

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

 

Related Article

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.