zoukankan      html  css  js  c++  java
  • iOS 四种延时的方法

    - (void)initBlock{
        //延时的方法
        //1:GCD延时 此方式在能够在參数中选择运行的线程。

    是一种非堵塞的运行方式,没有找到取消运行的方法。
        double delay1=2.0;//设置延时时间
        dispatch_time_t popTime=dispatch_time(DISPATCH_TIME_NOW, delay1 * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^{
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"GCD延时" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
            [alert show];
        });
        //2:NSTimer延时,此方式要求必须在主线程中运行。否则无效。是一种非堵塞的运行方式,能够通过NSTimer类的- (void)invalidate;取消运行。
        [NSTimer scheduledTimerWithTimeInterval:8.0f target:self selector:@selector(delayMethod2) userInfo:nil repeats:NO];
        //3:PerformSelector延时
        [self performSelector:@selector(delayMethod) withObject:nil afterDelay:5.0f];
        //4:NSThread 延时
        [NSThread sleepForTimeInterval:11.0f];
        [self delayMethod3];
    }
    - (void)delayMethod{
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"PerformSelector延时" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alert show];
    }
    - (void)delayMethod2{
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"NSTimer延时" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alert show];
    }
    - (void)delayMethod3{
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"NSThread延时" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alert show];
    }

  • 相关阅读:
    GridView里的数据转化为datatable进行数据传输
    asp.net给asp:button同时添加服务器事件和JS事件
    .msi文件安装出现2503、2502错误
    初识IO流——创建目录和文件
    WinRAR注册
    Ubuntu14.04.1 阿里apt源
    Pycharm远程调试
    Couldn't create temporary file to work with
    Ubuntu14中supervisor的安装及配置
    Ubuntu14.04安装pip及配置
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5224688.html
Copyright © 2011-2022 走看看