zoukankan      html  css  js  c++  java
  • IOS RunLoop浅析 一

    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
    复制代码
  • 相关阅读:
    POJ 1469 COURSES 二分图最大匹配
    POJ 1325 Machine Schedule 二分图最大匹配
    USACO Humble Numbers DP?
    SGU 194 Reactor Cooling 带容量上下限制的网络流
    POJ 3084 Panic Room 求最小割
    ZOJ 2587 Unique Attack 判断最小割是否唯一
    Poj 1815 Friendship 枚举+求最小割
    POJ 3308 Paratroopers 最小点权覆盖 求最小割
    1227. Rally Championship
    Etaoin Shrdlu
  • 原文地址:https://www.cnblogs.com/wuyuxin/p/7045577.html
Copyright © 2011-2022 走看看