iOS-GCD 延迟处理dispatch_after

本文详细介绍了dispatch_after函数在Objective-C中的使用方法,并通过具体示例展示了如何利用该函数实现2秒后的延时操作。此外,还结合了SVProgressHUD进行HUD提示框的展示与隐藏,进一步丰富了dispatch_after的实际应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

dispatch_after是在指定时间后,将执行加入到队列中。

dispatch_after的用法如下:

dispatch_time_t time=dispatch_time(DISPATCH_TIME_NOW, 2ull *NSEC_PER_SEC); //设置时间2秒
dispatch_after(time, dispatch_get_main_queue(), ^{
        //2秒后执行操作
        //添加操作
 });


也可以用以下写法:

dispatch_after_f(dispatch_time(DISPATCH_TIME_NOW, 2ull *NSEC_PER_SEC), dispatch_get_main_queue(), NULL, function);//function为自己定义的执行方法

dispatch_afer搭配hud使用起来效果更加

[SVProgressHUD showWithStatus:@"正在清理..."];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2ull * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
      [SVProgressHUD dismiss];
       [SVProgressHUD showSuccessWithStatus:@"清理成功"];
});

参考例子

转载于:https://www.cnblogs.com/tomandhua/p/5711368.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值