ios - 使用 AsyncDisplayKit 添加自定义按钮

标签 ios facebook uiwebview asyncdisplaykit

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

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

enter image description here

最佳答案

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

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

您试图在创建 block 时设置 block 中节点的框架,因此您试图在初始化期间设置其框架。这会导致你的问题。我认为当您使用 initWithViewBlock: 时,您实际上不需要在节点上设置框架:因为 ASDisplayNode 在内部使用该 block 直接创建其 _view 属性,该属性最终添加到 View 层次结构中。

我还注意到您正在从后台队列调用 addSubview: ,在调用该方法之前,您应该始终分派(dispatch)回主队列。为了方便起见,AsyncDisplayKit 还向 UIView 添加了 addSubNode: 。

我已经更改了您的代码以反射(reflect)更改,但我建议您在此处使用 ASTextNode。

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

关于ios - 使用 AsyncDisplayKit 添加自定义按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28831366/

相关文章:

ios - UIWebView 很慢

facebook - Facebook 的群聊是在 API 中实现的吗?

ios - Swift 中的 UIWebView 问题

iphone - UIWebView - 自动调整大小,以便不需要滚动(在 webView 本身中)

ios - 有什么可行的方法可以使用 UIWebView/WKWebView 在 UITableViewCell 中加载动态内容?

ios - NSUserDefaults 自定义对象 - 格式 : 200 (property lists cannot contain objects of type 'CFType' ) 的属性列表无效

ios - 在预设演示之前丢失对 transitioningDelegate 的引用

ruby-on-rails - 如何使用 fb_graph 从 facebook 邀请好友?

facebook - 如何通过 Facebook ID 获取 Facebook 用户的公开信息

来自 Objective C 的 C++ 方法