zoukankan      html  css  js  c++  java
  • iOS延时执行的几种方法

    博客

    iOS延时执行的几种方法

    热度 7已有 20486 次阅读 2012-6-1 11:16 |系统分类:ios

     
     
    1.最直接的方法performSelector:withObject:afterDelay:
    这种方法的缺点:每次要为延时写一个方法
     
    2.使用类别,用BOLCK执行

    [代码]c#/cpp/oc代码:

    01 @implementation NSObject (PerformBlockAfterDelay)
    02  
    03 - (void)performBlock:(void (^)(void))block
    04           afterDelay:(NSTimeInterval)delay
    05 {
    06     block = [[block copy] autorelease];
    07     [self performSelector:@selector(fireBlockAfterDelay:)
    08                withObject:block
    09                afterDelay:delay];
    10 }
    11  
    12 - (void)fireBlockAfterDelay:(void (^)(void))block {
    13     block();
    14 }
    15  
    16 @end
     
    3.使用GCD

    [代码]c#/cpp/oc代码:

    1 void RunBlockAfterDelay(NSTimeInterval delay, void (^block)(void))
    2 {
    3     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC*delay),
    4       dispatch_get_current_queue(), block);
    5 }
     
    4.可能是不太好的方法,用animation的completion参数

    [代码]c#/cpp/oc代码:

    1 [UIView animateWithDuration:0.0 delay:5.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
    2 } completion:^(BOOL finished) {
    3     //do stuff here
    4 }];
     
    5.使用NSOperationQueue,在应用程序的下一个主循环执行:

    [代码]c#/cpp/oc代码:

    1 [[NSOperationQueue mainQueue] addOperationWithBlock:aBlock];
     
    这个和调用performSelector: with afterDelay of 0.0f等价
  • 相关阅读:
    PostgreSQL pg_hba.conf 文件简析
    Centos 查看端口占用情况
    Nginx 从0开始学
    windows 杀死端口号对应进程
    MyBatis基础-05-缓存
    MyBatis基础-04-动态sql
    MyBatis基础-02
    SpringMVC基础-14-SpringMVC与Spring整合
    SpringMVC基础-13-SpringMVC运行流程
    SpringMVC基础-12-异常处理
  • 原文地址:https://www.cnblogs.com/hanyis/p/3660051.html
Copyright © 2011-2022 走看看