在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才会有效
网友评论