首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用AsyncDisplayKit添加自定义按钮

使用AsyncDisplayKit添加自定义按钮
EN

Stack Overflow用户
提问于 2015-03-03 12:03:29
回答 1查看 3.1K关注 0票数 2

我正在开发一个IOS应用程序。我使用Facebook AsyncDisplayKit库。我想在ASNodeCell Bu中设置一个按钮,当被块捕获时,我得到“变量‘节点”未初始化。如何在ASNodeCell中添加UIButton或UIWebView控件。请帮助我。

代码语言:javascript
复制
dispatch_queue_t _backgroundContentFetchingQueue;
    _backgroundContentFetchingQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

dispatch_async(_backgroundContentFetchingQueue, ^{
    ASDisplayNode *node = [[ASDisplayNode alloc] initWithViewBlock:^UIView *{
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button sizeToFit];
        node.frame = button.frame;
        return button;
    }];

                           // Use `node` as you normally would...
    node.backgroundColor = [UIColor redColor];

    [self.view addSubview:node.view];
});

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-09 10:53:43

注意,在您的示例中,不需要使用UIButton,您可以使用ASTextNode作为按钮,因为它是从ASControlNode继承的(ASImageNode也是如此)。这将在指南的第一页的底部描述:http://asyncdisplaykit.org/guide/。这还允许您在后台线程而不是主线程上进行文本调整(在主队列上执行您在示例中提供的块)。

为了完整起见,我还将评论您提供的代码。

在创建节点时,您尝试在块中设置节点的框架,所以在初始化时尝试在其上设置框架。这会引起你的问题。我不认为在使用initWithViewBlock时实际上需要在节点上设置框架:因为在内部,ASDisplayNode使用块直接创建其_view属性,该属性最终添加到视图层次结构中。

我还注意到您正在调用addSubview:在调用该方法之前,应该始终在后台队列中调度回主队列。为了方便起见,AsyncDisplayKit还将addSubNode:添加到UIView中。

我已经更改了您的代码,以反映这些更改,不过我建议您在这里使用ASTextNode。

代码语言:javascript
复制
dispatch_queue_t _backgroundContentFetchingQueue;
_backgroundContentFetchingQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

dispatch_async(_backgroundContentFetchingQueue, ^{
ASDisplayNode *node = [[ASDisplayNode alloc] initWithViewBlock:^UIView *{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button sizeToFit];
    //node.frame = button.frame; <-- this caused the problem
    return button;
}];

                       // Use `node` as you normally would...
node.backgroundColor = [UIColor redColor];

// dispatch to main queue to add to view
dispatch_async(dispatch_get_main_queue(),
    [self.view addSubview:node.view];
    // or use [self.view addSubnode:node];
  );
});
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28831366

复制
相关文章

相似问题

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