您的位置:首页 > 移动开发 > IOS开发

iOS 延迟调用某个方法的几种方法

2016-07-19 11:55 459 查看
//

//  ViewController.m

//  test_方法延迟调用

//

//  Created by 邹彦军 on 16/7/19.

//  Copyright © 2016年
邹彦军. All rights reserved.

//

#import "ViewController.h"

@interface
ViewController ()

@property (nonatomic,
strong) NSTimer* timer;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super
viewDidLoad];

    

    NSLog(@"delayMethodStart");

    [self
methodOnePerformSelector];

//        [self methodTwoNSTimer];

//        [self methodThreeSleep];

//        [self methodFourGCD];

//        [self methodFiveAnimation];

    NSLog(@"nextMethod");

}

// 动画完成的回调

- (void)methodFiveAnimation{

    [UIView
animateWithDuration:0
delay:2.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{

    } completion:^(BOOL finished) {

        [self
delayMethod];

    }];

}

// GCD

- (void)methodFourGCD{

    __weak
ViewController *weakSelf =
self;

    dispatch_time_t delayTime =
dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0
* NSEC_PER_SEC));

    <
4000
/span>dispatch_after(delayTime,
dispatch_get_main_queue(), ^{

        [weakSelf delayMethod];

    });

}

// 阻塞线程

- (void)methodThreeSleep{

    [NSThread
sleepForTimeInterval:2.0];

}

// 定时器

- (void)methodTwoNSTimer{

    self.timer = [NSTimer
scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(delayMethod)
userInfo:nil
repeats:NO];

}

// performSelector方法

- (void)methodOnePerformSelector{

    [self
performSelector:@selector(delayMethod)
withObject:nil
afterDelay:2.0];

}

// 被执行的方法

- (void)delayMethod{

    NSLog(@"delayMethodEnd");

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios