iOS用延时dispatch_after解决bug示例

iOS用延时dispatch_after解决bug示例_第1张图片
demo2.gif
  • 1.tableView 本地筛选,cell刷新时候出现如图的bug,注:cell只有一个时候,才会出现这个bug,如果不用延时,setContentOffset:CGPointZero代码无效
[self.tableView reloadData];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
     [self.tableView setContentOffset:CGPointZero animated:YES];
});
iOS用延时dispatch_after解决bug示例_第2张图片
demo.gif
  • 2.push到一个或者多个VC,返回回到指定首页,如图,注意:不是回到我的界面,而是发现TabBarItem
UITabBarController *vc=self.tabBarController;
[self backAction:^{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [vc setSelectedIndex:1];
    });
}];
  • 3.延迟加载,保证控件在其他控件的上面
// 延迟加载窗口,保证创建的这个窗口在所有窗口的最上面
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
      window_ = [[UIWindow alloc] init];
});
  • 4.延迟加载,保证rootViewController一定有值
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
   _ = delay(0.3) {
        if let rootVC = UIApplication.shared.keyWindow?.rootViewController {
            Helper.showStandardNoDismissDialog(viewController: rootVC, title: "请务必允许推送", message: "保证第一时间获取工单的反馈") {

                //aliyun移动推送
                self.initCloudPush()
            }
        }
    }
}
  • 5.使用便利方法初始化控制器,返回值不能保证立即的,为保证controller一定有值,可以延时调用
convenience init(price: String,completion: @escaping ((String) -> Void)) {
     self.init(type: .freePay,title:nil, text: price, completion: nil)
     self.freePayBlock = completion
}
let popupVC = DXPopupDialog.init(price: price, completion: completions)
_ = delay(0.1) {
     viewController.present(popupVC, animated: true, completion: nil)
}

你可能感兴趣的:(iOS用延时dispatch_after解决bug示例)