5

我只是想把头绕在 Grand Central Dispatch (GCD) 上,但我似乎无法找到答案

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

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

但是您是否允许在视图元素实际添加到视图之前对其进行处理?换句话说,这是否允许?

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];
    }

});
4

1 回答 1

3

是的你可以。最佳做法是不更新. 初始化没问题。

可以在此博客文章中阅读示例。它几乎与您正在尝试的内容一致。

于 2012-09-25T19:58:56.023 回答