iOS开发中方法延迟执行的几种方式
2016-05-12 09:50
405 查看
Method1. performSelector方法
Method2. NSTimer定时器
Method3. NSThread线程的sleep
Method4. GCD
公用延迟执行方法- (void)delayMethod{ NSLog(@"delayMethodEnd");}
Method1:performSelector
[self performSelector:@selector(delayMethod) withObject:nil/*可传任意类型参数*/ afterDelay:2.0];
注:此方法是一种非阻塞的执行方式,未找到取消执行的方法。
程序运行结束
2015-08-31 10:56:59.361 CJDelayMethod[1080:39604] delayMethodStart2015-08-31 10:56:59.363 CJDelayMethod[1080:39604] nextMethod2015-08-31 10:57:01.364 CJDelayMethod[1080:39604] delayMethodEnd
Method2:NSTimer定时器
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];
注:此方法是一种非阻塞的执行方式,
取消执行方法:
- (void)invalidate;即可
程序运行结束
2015-08-31 10:58:10.182 CJDelayMethod[1129:41106] delayMethodStart2015-08-31 10:58:10.183 CJDelayMethod[1129:41106] nextMethod2015-08-31 10:58:12.185 CJDelayMethod[1129:41106] delayMethodEnd
Method3:NSThread线程的sleep
[NSThread sleepForTimeInterval:2.0];
注:此方法是一种阻塞执行方式,建议放在子线程中执行,否则会卡住界面。但有时还是需要阻塞执行,如进入欢迎界面需要沉睡3秒才进入主界面时。
没有找到取消执行方式。
程序运行结束
2015-08-31 10:58:41.501 CJDelayMethod[1153:41698] delayMethodStart2015-08-31 10:58:43.507 CJDelayMethod[1153:41698] nextMethod
Method4:GCD
__block ViewController/*主控制器*/ *weakSelf = self;
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC)); dispatch_after(delayTime, dispatch_get_main_queue(), ^{ [weakSelf delayMethod]; });`
注:此方法可以在参数中选择执行的线程,是一种非阻塞执行方式。没有找到取消执行方式。
程序运行结束
2015-08-31 10:59:21.652 CJDelayMethod[1181:42438] delayMethodStart2015-08-31 10:59:21.653 CJDelayMethod[1181:42438] nextMethod2015-08-31 10:59:23.653 CJDelayMethod[1181:42438] delayMethodEnd
完整代码参见:
//
// ViewController.m
// CJDelayMethod
//
// Created by 陈杰 on 8/31/15.
// Copyright (c) 2015 chenjie. All rights reserved.
//
import "ViewController.h"
@interface ViewController ()@property (nonatomic, strong) NSTimer timer;
@end
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSLog(@"delayMethodStart"); [self methodOnePerformSelector];// [self methodTwoNSTimer];// [self methodThreeSleep];// [self methodFourGCD]; NSLog(@"nextMethod");}
- (void)methodFiveAnimation{ [UIView animateWithDuration:0 delay:2.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ } completion:^(BOOL finished) { [self delayMethod]; }];}
- (void)methodFourGCD{ __block ViewController
weakSelf = self; dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)); dispatch_after(delayTime, dispatch_get_main_queue(), ^{ [weakSelf delayMethod]; });}
- (void)methodThreeSleep{ [NSThread sleepForTimeInterval:2.0];}
- (void)methodTwoNSTimer{ NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];}
- (void)methodOnePerformSelector{ [self performSelector:@selector(delayMethod) withObject:nil/*可传任意类型参数*/ afterDelay:2.0];}
- (void)delayMethod{ NSLog(@"delayMethodEnd");}
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
相关文章推荐
- iOS开发之使用CocoaPods开发并打包静态库
- IOS之基础动画
- 【代码笔记】iOS-评分系统(小星星)
- 界面之间各种传值
- iOS9适配
- IOS中通知中心(NSNotificationCenter)的使用总结
- iOS对象间传值
- 《Motion Design for iOS》(十六)
- iOS应用程序中通过dispatch队列控制线程执行的方法
- [绍棠] iOS远程推送原理及实现过程
- iOS正则表达式运用
- iOS开发之AFNetworking 3.0.4使用
- iOS 集合的深复制与浅复制
- iOS开发——超链接富文本
- 关于友盟分享时“打开手机QQ失败”的一般解决方法
- IOS开发中的时间、日期问题
- 加速计原理 IOS4.0之前
- iOS中pch文件更简便的位置配置
- [10秒学会] - iOS tableView中headView和footView悬停方式
- iOS开发添加楷体字体(图解)