淺談iOS開發(fā)中方法延遲執(zhí)行的幾種方式

Method1. performSelector方法

Method2. NSTimer定時器

Method3. NSThread線程的sleep

Method4. GCD

公用延遲執(zhí)行方法

- (void)delayMethod{
    NSLog(@"delayMethodEnd");
}
Method1:performSelector

[self performSelector:@selector(delayMethod) withObject:nil/*可傳任意類型參數(shù)*/ afterDelay:2.0];
注:此方法是一種非阻塞的執(zhí)行方式,未找到取消執(zhí)行的方法。

程序運(yùn)行結(jié)束
2015-08-31 10:56:59.361 PPDelayMethod[1080:39604] delayMethodStart
2015-08-31 10:56:59.363 PPDelayMethod[1080:39604] nextMethod
2015-08-31 10:57:01.364 PPDelayMethod[1080:39604] delayMethodEnd
Method2:NSTimer定時器

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];
注:此方法是一種非阻塞的執(zhí)行方式,
取消執(zhí)行方法:- (void)invalidate;即可

程序運(yùn)行結(jié)束
2015-08-31 10:58:10.182 PPDelayMethod[1129:41106] delayMethodStart
2015-08-31 10:58:10.183 PPDelayMethod[1129:41106] nextMethod
2015-08-31 10:58:12.185 PPDelayMethod[1129:41106] delayMethodEnd
Method3:NSThread線程的sleep

[NSThread sleepForTimeInterval:2.0];
注:此方法是一種阻塞執(zhí)行方式,建議放在子線程中執(zhí)行,否則會卡住界面。但有時還是需要阻塞執(zhí)行,如進(jìn)入歡迎界面需要沉睡3秒才進(jìn)入主界面時。
沒有找到取消執(zhí)行方式。

程序運(yùn)行結(jié)束
2015-08-31 10:58:41.501 PPDelayMethod[1153:41698] delayMethodStart
2015-08-31 10:58:43.507 PPDelayMethod[1153:41698] nextMethod
Method4:GCD

__block ViewController/*主控制器*/ *weakSelf = self;

dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延遲執(zhí)行時間*/ * NSEC_PER_SEC));

dispatch_after(delayTime, dispatch_get_main_queue(), ^{
    [weakSelf delayMethod];
});`
注:此方法可以在參數(shù)中選擇執(zhí)行的線程,是一種非阻塞執(zhí)行方式。沒有找到取消執(zhí)行方式。

程序運(yùn)行結(jié)束
2015-08-31 10:59:21.652 PPDelayMethod[1181:42438] delayMethodStart
2015-08-31 10:59:21.653 PPDelayMethod[1181:42438] nextMethod
2015-08-31 10:59:23.653 PPDelayMethod[1181:42438] delayMethodEnd
完整代碼參見:

//
// ViewController.m
// PPDelayMethod
//
// Created by PP on 8/31/15.
// Copyright (c) 2015 PP. 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/*可傳任意類型參數(shù)*/ afterDelay:2.0];
}
- (void)delayMethod{
    NSLog(@"delayMethodEnd");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容