首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >异步加载UIViews

异步加载UIViews
EN

Stack Overflow用户
提问于 2012-09-12 00:09:29
回答 1查看 2K关注 0票数 2

我正在尝试异步加载视图。问题是要加载的视图框架依赖于异步加载的数据。换句话说,有一些很长的计算来决定在哪里实际显示UIViews。

我知道在线程中实际显示UIView时会出现问题,您应该始终将它们重新加载到主线程中,所以这是我一直在尝试的代码:

代码语言:javascript
复制
asyncQueue = [[NSOperationQueue alloc] init];
[asyncQueue addOperationWithBlock:^{
    // Do work to load the UIViews and figure out where they should be
    UIButton *test = [[UIButton alloc] initWithFrame:[self doWorkToGetFrame]];

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [self addSubview:test];
        }
    }];
}];

这些都驻留在一个UIView容器中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-12 00:48:09

尝试如下所示:

代码语言:javascript
复制
UIButton *test = [[UIButton alloc] initWithFrame:CGRectZero];
test.hidden = YES;
test.alpha = 0.0f;
[self addSubview:test];

dispatch_queue_t downloadQueue = dispatch_queue_create("myDownloadQueue",NULL);
dispatch_async(downloadQueue, ^
{
  // do work to load UIViews and calculate frame
  CGRect frameButton = ...;

  dispatch_async(dispatch_get_main_queue(), ^
  {
    test.hidden = NO; 
    [UIView animateWithDuration:0.4f animations:^
    {
     test.frame = frameButton;
     test.alpha = 1.0f;
    } 
    completion:^(BOOL finished){}];


  });
});

dispatch_release(downloadQueue);

这会将您的按钮添加到主线程中,但最初会使其不可见。背景工作完成后,您将使其可见,并使用动画设置帧。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12373850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档