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: 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: 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: 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: target:self selector:@selector(test) userInfo:nil repeats:YES];
} -(void)test{ NSLog(@"旭宝爱吃鱼"); } @end

IOS RunLoop浅析 一的更多相关文章

  1. IOS RunLoop浅析 三

    经过两篇的介绍我想对RunLoop应该有了简单的了解,至少不至于一无所知. 在这篇我想对“CFRunLoopObserverRef”做一下简单的补充. 在补充之前先说一下. 在现在的开发中已经很少见到 ...

  2. IOS RunLoop浅析 二

    上一篇我们说了runloop 的几种模式,那么我们在模式中又要做些什么呢??? 模式中有三个模块: 事件源(输入源) Source Source: 按照官方文档分类 Port-Based Custom ...

  3. IOS 网络浅析-(十三 SDWebImage 实用技巧)

    IOS 网络浅析-(十三 SDWebImage 实用技巧) 首先让我描述一下为了什么而产生的实用技巧.(在TableView.CollectionView中)当用户所处环境WiFi网速不够快(不能立即 ...

  4. iOS runloop 资源汇总-b

    RunLoop 是 iOS 和 OSX 开发中非常基础的一个概念,这篇文章将从 CFRunLoop 的源码入手,介绍 RunLoop 的概念以及底层实现原理.之后会介绍一下在 iOS 中,苹果是如何利 ...

  5. iOS Runloop理解

    一.RunLoop的定义 当有持续的异步任务需求时,我们会创建一个独立的生命周期可控的线程.RunLoop就是控制线程生命周期并接收事件进行处理的机制. RunLoop是iOS事件响应与任务处理最核心 ...

  6. iOS RunLoop详解

    1. RunLoop简介 1.1 什么是RUnLoop 可以理解为字面的意思:Run表示运行,Loop表示循环.结合在一起就是运行的循环.通常叫做运行循环. RunLoop实际上是一个对象,这个对象在 ...

  7. iOS runLoop 理解

    目录 概述 run loop modes 一.概述 run loop叫事件处理循环,就是循环地接受各种各样的事件.run loop是oc用来管理线程里异步事件的工具.一个线程通过run loop可以监 ...

  8. iOS线程浅析

    一.线程概述 1. iOS里面的线程按种类可分为同步线程和异步线程.同步线程指调用同步线程的地方必须等到同步线程运行完成才干够继续向下运行.而调用异步线程的地方则在运行完调用异步线程的语句后就能够继续 ...

  9. iOS Runloop 消息循环

    介绍 Runloop是一种事件监听循环,可以理解成一个while死循环,监听到事件就起来,没有就休息. Runloop可以在不同模式下进行切换,iOS有五种模式,其中UIInitializationR ...

随机推荐

  1. ES6 - Note6:Set与Map

    Set和Map是ES6中新增的数据结构,Set是集合,无序唯一,Map类似于对象,也是"key-value"形式,但是key不局限于字符串. 1.Set的用法 Set是构造函数,可 ...

  2. MVC中处理表单提交的方式(Ajax+Jquery)

    MVC中处理表单有很多种方法,这里说到第一种方式:Ajax+Jquery 先看下表单: <form class="row form-body form-horizontal m-t&q ...

  3. ReactJS分析之入口函数render

    前言 在使用React进行构建应用时,我们总会有一个步骤将组建或者虚拟DOM元素渲染到真实的DOM上,将任务交给浏览器,进而进行layout和paint等步骤,这个函数就是React.render() ...

  4. 深入seajs源码系列二

    模块类和状态类 参照上文的demo,我们结合源码分析在简单的API调用的背后,到底使用了什么技巧来实现各个模块的依赖加载以及模块API的导出. 首先定义了一个Module类,对应与一个模块 funct ...

  5. SYSTEM表空间AUD$使用空间过大问题处理

    问题现象:SYSTEM表空间使用率超99%,前期设计时SYSTEM表空间是10G固定大小不允许自动扩展. 1.查询SYSTEM表空间的使用率 set linesize 200 col TABLESPA ...

  6. Access数据库多表连接查询

    第一次在Access中写多表查询,就按照MS数据库中的写法,结果报语法错,原来Access的多表连接查询是不一样的 表A.B.C,A关联B,B关联C,均用ID键关联 一般写法:select * fro ...

  7. [Web API] Web API 2 深入系列(4) Action的选择

    目录 ApiController HttpActionDescriptor IHttpActionSelector ApiController 在上节中,讲到如何选择并激活对应的IHttpContro ...

  8. Win10 UWP 开发系列:使用多语言工具包让应用支持多语言

    之前我在一篇blog中写过如何使用多语言工具包,见http://www.cnblogs.com/yanxiaodi/p/3800767.html 在WinEcos社区也发布过一篇详细的文章介绍多语言工 ...

  9. linux使用心得(持续更新)

    ! 查看发行版本信息 lsb_release -a uname -a   以下方法只适合redhat和centos cat /etc/redhat-release rpm -q redhat-rele ...

  10. 【C#进阶系列】26 计算限制的异步操作

    什么是计算限制的异步操作,当线程在要使用CPU进行计算的时候,那么就叫计算限制. 而对应的IO限制就是线程交给IO设备(键鼠,网络,文件等). 第25章线程基础讲了用专用的线程进行计算限制的操作,但是 ...