我只是想把头放在大中央调度(GCD)上,而我似乎无法找到答案

因此,我知道通常不应该从主线程以外的任何线程更新UIView元素。所以这是非法的:

dispatch_async(workerQueue, ^{
    self.view.backgroundColor = [UIColor redColor];
});

但是,您是否允许在将 View 元素实际添加到 View 之前对其进行处理?换句话说,这是允许的吗?
dispatch_async(workerQueue, ^{
    UIButton *thisLevelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    thisLevelButton.frame = CGRectMake(x, y, BUTTON_WIDTH, BUTTON_HEIGHT);
    thisLevelButton.tag = kLevelButtonTags + j;


    int levelState = [[[thisCat objectForKey:[levelNamesInCat objectAtIndex:j]] objectAtIndex:4] intValue];

    [thisLevelButton setBackgroundImage:[UIImage imageNamed:@"ccLSButton50lock"]forState:UIControlStateNormal];
    thisLevelButton.alpha = 0.5;
    [thisLevelButton addTarget:self action:@selector(unlockButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.view addSubview:thisLevelButton];
    }

});

最佳答案

是的你可以。最佳做法是不要更新。初始化很好。

An example can be read in this blog post.几乎与您尝试的内容差不多。

10-07 22:47