zoukankan      html  css  js  c++  java
  • 【转】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.
  • 相关阅读:
    HDU 3081 Marriage Match II
    HDU 4292 Food
    HDU 4322 Candy
    HDU 4183 Pahom on Water
    POJ 1966 Cable TV Network
    HDU 3605 Escape
    HDU 3338 Kakuro Extension
    HDU 3572 Task Schedule
    HDU 3998 Sequence
    Burning Midnight Oil
  • 原文地址:https://www.cnblogs.com/wi100sh/p/5604991.html
Copyright © 2011-2022 走看看