【转】iOS-延迟操作方法总结
原文网址:http://lysongzi.com/2016/01/30/iOS-%E5%BB%B6%E8%BF%9F%E6%93%8D%E4%BD%9C%E6%96%B9%E6%B3%95%E6%80%BB%E7%BB%93/
在实际应用中,有时候我们会需要延时执行某些操作,所以我们这里总结了四种延迟操作的方法,并简要分析了每种方法的异同。
NSObject的相关方法
第一种方法是使用NSObject类的performSelector:withObject:afterDelay:方法,该方法可以指定延迟一段时间后执行某个特定方法。这个方法必须在主线程中执行,否则会无效。同时该方法也是非阻塞的,无法取消延迟指向?(待考证)。
[self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.0];
NSTimer
第二种方法可以使用NSTimer,该类也主要有三种方法生成一个NSTimer对象,其中我们可以更具需要选用合适的方法。通过该方法执行的延时操作需要在主线程中运行,也是非阻塞的方式。如果需要取消延迟执行操作,可以调用invalidate:操作停止一个计时器对象。
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];
GCD
通过GCD的方式执行延时操作是比较灵活的,因为这种方法既可以在主线程也可以在子线程上执行,且为非阻塞的方式,不足之处是尚无取消延迟操作的方法。
double delayInSeconds = 3.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^{
[self delayMethod];
});
NSThread
这种方法是很简单除暴的,就是让线程休眠一段时间,然后再执行需要的操作。因此这种方法是阻塞式的,不推荐在主线程中使用,因为会造成UI卡顿。可以在子线程是使用。暂无取消方法。
[NSThread sleepForTimeInterval:1.0];
[self delayMethod];
Demon
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//第一种方法
//在主线程执行,否则无效;非阻塞;无法取消
[self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.0];
//第二种方法
//主线程中执行,否则无效;非阻塞;可以调用invalidate方法取消
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];
//第三种方法
//可选择执行的线程;非阻塞;无法取消
double delayInSeconds = 3.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^{
[self delayMethod];
});
//第四种方法
//主线程子线程都可以;阻塞;无法取消
[NSThread sleepForTimeInterval:1.0];
[self delayMethod];
}
- (void)delayMethod
{
NSLog(@"Excute.");
}
@end
然后哦我们可以看到结果输出.
2016-01-30 20:54:27.822 DelayPerformSelector[2079:319217] Excute.
2016-01-30 20:54:27.830 DelayPerformSelector[2079:319217] Excute.
2016-01-30 20:54:28.820 DelayPerformSelector[2079:319217] Excute.
2016-01-30 20:54:29.818 DelayPerformSelector[2079:319217] Excute.
【转】iOS-延迟操作方法总结的更多相关文章
- iOS 延迟执行
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<#delayInSeconds#> * NSEC_PER_SEC)), ...
- iOS 延迟执行代码
//延迟执行 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)( * NSEC_PER_SEC)),dispatch_get_main ...
- ios 延迟调用 && UIImageView && UILabel && UISegmentedControl && UISwitch && UISlider
// // ViewController.m // UI_Lesson3 // // Created by archerzz on 15/8/13. // Copyright (c) 2015 ...
- iOS延迟执行方法
swift 4.0中dispatch_async,dispatch_after的使用 2018年03月28日 16:15:44 xiao_yuly 阅读数:3576 版权声明:本文为博主原创文章,未经 ...
- ios -- 延迟3秒触发performSelector
[self performSelector:@selector(changeText:) withObject:@"Happy aha" afterDelay:3];
- ios h5 app avalon tap点击事件失效及点击延迟300ms问题解决方法
1.ios h5 app avalon tap事件失效 使用MUI制作app界面,使用avalon.js渲染数据,发现在(Android上正常)ios上运行时容器div的avalon的ms-on-ta ...
- iOS AudioQueue机制的延迟问题探究
关键字:VOIP,AudioUnit,AudioQueue,RemoteIO问题描述VOIP通话,iOS底层音频方式采用AudioUnit机制,本来也挺好,但是会有遇到CS域来电时RemoteIO挂死 ...
- vue项目引入FastClick组件解决IOS系统下h5页面中的按钮点击延迟,连续点击无反应的问题
异常描述: ios系统手机中访问h5页面,按钮点击有延迟,连续点击卡顿.无反应. 异常原因: 这要追溯至 2007 年初.苹果公司在发布首款 iPhone 前夕,遇到一个问题:当时的网站都是为大屏幕设 ...
- ios开发之-- 延迟执行方法
延迟执行的几种方法,分享一下. 1.performSelector(NSObject)方法 2.NSTimer方法 3.GCD方法 4.sleep(NSThread)方法 1.performSe ...
- iOS 设置 延迟执行 与 取消延迟执行 方法 以及对 run loop 初步认识
之前开发过程中经常会有需求会使用 NSObject中的"performSelector:withObject:afterDelay:"做方法延迟执行的处理, 但是 还没有什么地方需 ...
随机推荐
- sigleSchool 存储过程例1
CREATE OR REPLACE PROCEDURE SINGLSCHOOL( PICIID IN VARCHAR2, SCHOOLID IN NUMBER, SCHETYPE IN number, ...
- object-c 入门基础篇
原地址:http://www.cnblogs.com/moonvan/archive/2011/10/13/2210498.html 一.Objective-C与C的渊源 Objective-C诞生于 ...
- POJ 1674
#include<iostream>//cheng da cai zi 08 .11 .13 using namespace std; int main() { int digit_num ...
- POJ1442Black Box
http://poj.org/problem?id=1442 题意 : 题目中对给出的数字有两种操作ADD(I)操作,将ADD括号里的数字 I 加到数列里边去,然后是自动排好序的,每一个数列前边都会有 ...
- hdu 4701 Game 博弈论
思路: ▶ 设 win(i,x,y) 表示当前可以买的物品是 i,先手有 x 元,后 手有 y 元时,先手是否必胜 ▶ win(i,x,y) ⇐⇒∃j((j > i)∧(x ≥ si−sj)∧¬ ...
- IE插件DebugBar如何安装及使用
DebugBar插件是一款功能很强大的IE插件,用户可以从各个不同的角度剖析Web页面内部,包括页面 详细代码.CSS样式表.所有链接.所有图片代码.脚本信息等等,不管你是编程大虾还是IT新人都和适合 ...
- asp.net后台获取路径的各种方法归纳
asp.net后台获取路径的各种方法归纳 1.Request.CurrentExecutionFilePath 获取当前请求的虚拟路径,不同于 FilePath,差别在于如果请求已在服务器代 ...
- Hadoop格式化HDFS报错java.net.UnknownHostException: localhost.localdomain: localhost.localdomain
异常描述: 在对HDFS格式化,执行hadoop namenode -format命令时,出现未知的主机名的问题,异常信息如下所示: [shirdrn@localhost bin]$ hadoop n ...
- zend studio 10破解/汉化(转发)
转发:http://blog.csdn.net/qq1355541448/article/details/16807429 Zend Studio 10正式版破解及汉化 2013年03月12日 ⁄ P ...
- Unity UGUI —— 无限循环List(转载)
using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; ...