UIView在iOS中的异步加载, 以及在异步执行webView里面的JS弹窗阻塞问题

在iOS开发中,所有的控件加载都是主线程异步渲染的

例如在controller的touchesBegan方法中, 加载控件并测试打印

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    NSInteger totalCount = 30000;
    NSInteger colNum = 3000;
    
    for (NSInteger i = 0; i < totalCount; i++) {
    
        NSInteger col = i / colNum;
        NSInteger row = i % colNum;
        CGFloat itemW = 20;
        CGFloat itemH = 20;
        
        CGFloat x =  (2 + itemW)*col;
        CGFloat y = 60 + (2 + itemH)*row;
        
        UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(x, y, itemW, itemH)];
        lbl.textAlignment = NSTextAlignmentCenter;
        lbl.text = [NSString stringWithFormat:@"%ld",i];
        lbl.backgroundColor = [UIColor orangeColor];
        [self.view addSubview: lbl];
    }
    NSLog(@"开始");
}

可以发现, 在for循环执行后,打印了"开始"后,页面才会显示lable控件,

案例2:
如创建tableView或者collectionView后立马执行ContentOffset往往没有效果,必须异步执行ContentOffset才能生效,这也是因为collectionView是异步渲染的,必须在collectionView渲染后设置ContentOffset才会有效

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容