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 ...
随机推荐
- NuGet程序包安装SQLite后完全抽离出SQLite之入门介绍及注意事项,你真的懂了吗?
前言 近几天的几篇文章讲的内容非前面内容如系列的讲解,这几天文章都是我在项目中遇到的问题以及重新学习的知识,所以和大家分享一下,关于SQLite的文章多如牛毛,但是有些大多已经过时,为什么说过时,之前 ...
- VS 2015 Enterprise第二大坑
前言 继上篇文章之后,你会继续跌进大坑,这个坑困扰我一上午,同时也会让你大跌眼镜,如果你遇到了,那么恭喜你提升自身能力和解决能力的时机到了,当然你可以通过本文继续少走不必要的弯路[我也是无意中发现的捷 ...
- EntityFramework 7 Left Join Where Select 奇怪问题
这篇博文纪录一下:使用 EF7,当 Linq 查询中使用 "Left Join" 语法(DefaultIfEmpty),Where Select 不同条件语法实现,出现的不同问题. ...
- php广告图片循环播放 幻灯片效果
<!DOCTYPE> <html> <head> <meta http-equiv="content-type" content=&quo ...
- FusionChart 水印破解方法(代码版)
网上一直找不到有关去除水印的方法! 正好做项目要用到这个鬼东西,折腾了一天,总算破解成功,把方法告诉大家. 先看下破解前的效果. 这是个很烦人的东西,而且有一个更加让人烦人的地方, 就是根本无法买!为 ...
- Android测试提升效率批处理脚本(二)
前言: 前面放出过一次批处理,本次再放出一些比较有用的批处理(获得当前包名.查看APP签名信息等),好长时没来写博客了,简单化,请看正文,更多脚本尽请期待~~~(不定期) 目录 1.[手机录屏(安卓4 ...
- OpenGL新手框架
开始学习用OpenGL,也就想显示一些点,以为挺简单的,哎,看了两天才会画三维的点,做个总结. 使用OpenGL的基本流程 int main(int argv, char *argc[]) { //初 ...
- iOS 商品倒计时 限时特价 限时优惠 功能的封装
最近项目中多个页面用到了 商品特价倒计时的功能 为了偷懒 于是自己封装了一个限时抢购 倒计时的view 代码实现如下: 定向价 限时特价 模型代码实现: #pragma mark 商品定向价模型 @ ...
- iOS页面间传值的一些方式总结
废话不多说,直接进入主题: 这里要说的方式有6种:1.属性传值 2.block 3.delegate 4.UserDefault 5.单例 6.通知(篇幅原因我只写核心代码,如果看不懂可以直接在最下面 ...
- 在 Ubuntu 上安装 Android Studio
在 Ubuntu 上安装 Android Studio http://www.linuxidc.com/Linux/2013-05/84812.htm 打开terminal,输入以下命令 sudo a ...