IOS RunLoop淺析 一,iosrunloop淺析

來源:互聯網
上載者:User

IOS RunLoop淺析 一,iosrunloop淺析

RunLoop猶如其名迴圈。

RunLoop 中有多重模式。

在一個“時刻”只能值執行一種模式。

因此在使用RunLoop時要注意所實現的效果有可能不是你想要的。

在這裡用NSTimer展示一下Runloop的簡單實現。

在故事板中添加一個TextView(用於測試)

 

我們吧nstimer加入到NSDefaultRunLoopMode模式中

 

在上面我們可以很清晰的看到,當我們滾動TextView的時候,nstimer不在執行。

////  ViewController.m//  CX RunLoop淺析////  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];    //添加到預設的runloop中    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];        [timer fire];}                       -(void)test{    NSLog(@"旭寶愛吃魚");    }@end

我們吧nstimer加入到UITrackingRunLoopMode模式中

 

在上面我們可以很清晰的看到,當我們滾動TextView的時候,nstimer執行。

////  ViewController.m//  CX RunLoop淺析////  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];    //添加到預設的runloop中    [[NSRunLoop currentRunLoop]addTimer:timer forMode:UITrackingRunLoopMode];        [timer fire];}                       -(void)test{    NSLog(@"旭寶愛吃魚");    }@end

我們吧nstimer加入到NSRunLoopCommonModes模式中

 

在上面我們可以很清晰的看到,當我們滾動與不滾動TextView的時候,nstimer都執行。

////  ViewController.m//  CX RunLoop淺析////  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];    //添加到預設的runloop中    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];        [timer fire];}                       -(void)test{    NSLog(@"旭寶愛吃魚");    }@end

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(test) userInfo:nil repeats:YES];

自動添加到runloop 並且預設為NSDefaultRunLoopMode.

但是我們可以通過與上面相同的方法改變模式。

////  ViewController.m//  CX RunLoop淺析////  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(@"旭寶愛吃魚");    }@end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.