iOS 延時調用

來源:互聯網
上載者:User

標籤:

Method1. performSelector方法Method2. NSTimer定時器Method3. NSThread線程的sleepMethod4. 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.}



文/笨笨編程(簡書作者)
原文連結:http://www.jianshu.com/p/6ed28a29b391
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。

iOS 延時調用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.