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: 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浅析 一的更多相关文章
- IOS RunLoop浅析 三
经过两篇的介绍我想对RunLoop应该有了简单的了解,至少不至于一无所知. 在这篇我想对“CFRunLoopObserverRef”做一下简单的补充. 在补充之前先说一下. 在现在的开发中已经很少见到 ...
- IOS RunLoop浅析 二
上一篇我们说了runloop 的几种模式,那么我们在模式中又要做些什么呢??? 模式中有三个模块: 事件源(输入源) Source Source: 按照官方文档分类 Port-Based Custom ...
- IOS 网络浅析-(十三 SDWebImage 实用技巧)
IOS 网络浅析-(十三 SDWebImage 实用技巧) 首先让我描述一下为了什么而产生的实用技巧.(在TableView.CollectionView中)当用户所处环境WiFi网速不够快(不能立即 ...
- iOS runloop 资源汇总-b
RunLoop 是 iOS 和 OSX 开发中非常基础的一个概念,这篇文章将从 CFRunLoop 的源码入手,介绍 RunLoop 的概念以及底层实现原理.之后会介绍一下在 iOS 中,苹果是如何利 ...
- iOS Runloop理解
一.RunLoop的定义 当有持续的异步任务需求时,我们会创建一个独立的生命周期可控的线程.RunLoop就是控制线程生命周期并接收事件进行处理的机制. RunLoop是iOS事件响应与任务处理最核心 ...
- iOS RunLoop详解
1. RunLoop简介 1.1 什么是RUnLoop 可以理解为字面的意思:Run表示运行,Loop表示循环.结合在一起就是运行的循环.通常叫做运行循环. RunLoop实际上是一个对象,这个对象在 ...
- iOS runLoop 理解
目录 概述 run loop modes 一.概述 run loop叫事件处理循环,就是循环地接受各种各样的事件.run loop是oc用来管理线程里异步事件的工具.一个线程通过run loop可以监 ...
- iOS线程浅析
一.线程概述 1. iOS里面的线程按种类可分为同步线程和异步线程.同步线程指调用同步线程的地方必须等到同步线程运行完成才干够继续向下运行.而调用异步线程的地方则在运行完调用异步线程的语句后就能够继续 ...
- iOS Runloop 消息循环
介绍 Runloop是一种事件监听循环,可以理解成一个while死循环,监听到事件就起来,没有就休息. Runloop可以在不同模式下进行切换,iOS有五种模式,其中UIInitializationR ...
随机推荐
- 回忆:#define的用法
ANSI C规定:#前可以有空格或者tab,#和指令其余部分之间也可以有空格,可以出现在任何地方,作用域从定义处到文件结尾. 因为预处理开始前,系统会删除反斜线和换行符的组合,故可以把指令扩展到几个物 ...
- 跨域post请求实现方案小结--转
[名词解释] 跨域:https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript 同源策略 ...
- opendaylight的Beryllium安装
1.首先安装jdk #sudo apt-get install openjdk-7-jdk 2.安装vim编辑工具 #sudo apt-get install vim 3.编辑~/.bashrc ...
- ECMAScript 5中属性的特性值
这是<JavaScript高级程序设计(第三版)>第六章相关内容的总结. ECMAScript中有两种属性:数据属性和访问器属性.每种属性都有四个特性值. 数据属性的四个特性值: [[Co ...
- 【Python实战】Pandas:让你像写SQL一样做数据分析(一)
1. 引言 Pandas是一个开源的Python数据分析库.Pandas把结构化数据分为了三类: Series,1维序列,可视作为没有column名的.只有一个column的DataFrame: Da ...
- Hexo折腾记
如果时间可以静止,我希望就停在此刻. 前言 博主之前也有折腾wordpress和jekyll可对于一个前端er来说,wordpress让人没法尽兴,因为不知道该如何添加自己的代码.而jekyll就太麻 ...
- onclick标签变成小手状
style=“cursor: pointer;”
- 从零开始学Python第一周:Python基础(上)
Python语法基础(上) 一,Python的变量 (1)创建变量 变量的含义:存储信息的地方 创建变量并赋值 x = 1 print x x = 123 #再次赋值 print x (2)使用变量 ...
- 从头开始搭建一个dubbo+zookeeper平台
本篇主要是来分享从头开始搭建一个dubbo+zookeeper平台的过程,其中会简要介绍下dubbo服务的作用. 首先,看下一般网站架构随着业务的发展,逻辑越来越复杂,数据量越来越大,交互越来越多之后 ...
- 深入浅出node(3) 异步I/O
这篇主要整理深入浅出Node.js第三章 异步I/O 一) 异步I/O的原因 二)异步I/O实现现状 2.1 异步I/O与非阻塞I/O 2.2 轮询 2.3 理想的非阻塞异步I/O 2.4 现实的异步 ...