添加自定义按钮与AsyncDisplayKit [英] Add custom Button with AsyncDisplayKit

查看:188
本文介绍了添加自定义按钮与AsyncDisplayKit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个IOS应用程序。我使用Facebook AsyncDisplayKit库。我想要ASNodeCell中的一个按钮我得到了变量节点被块拦截时未初始化。如何在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。框架;
返回按钮;
}];

//通常使用`node` ...
node.backgroundColor = [UIColor redColor];

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

http://asyncdisplaykit.org/guide/ 。这也将允许您在后台线程上执行文本大小,而不是主线程(您在示例中提供的块在主队列上执行)。



为了完整,我还会对您提供的代码发表评论。



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



我也注意到你正在调用addSubview:从后台队列中,你应该总是在调用该方法之前调回到主队列。为方便起见,AsyncDisplayKit还将addSubNode添加到UIView。



我已经改变了你的代码,以反映这些变化,尽管我建议您在这里使用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;< - 这导致问题
返回按钮;
}];

//通常使用`node` ...
node.backgroundColor = [UIColor redColor];

//发送到主队列以添加到
dispatch_async (dispatch_get_main_queue(),
[self.view addSubview:node.view];
//或使用[self.view addSubnode:node];
);
});


I am developing an IOS application. I use Facebook AsyncDisplayKit library. I want to a button in ASNodeCell Bu I got "Variable 'node' is uninitialized when captured by block. How can I add UIButton or UIWebView control in ASNodeCell. Please help me

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

解决方案

Note that in your case there's no need to use UIButton, you can use ASTextNode as a button since it inherits from ASControlNode (same goes for ASImageNode). This is described at the bottom of the first page of the guide: http://asyncdisplaykit.org/guide/. That will also allow you to do the text sizing on the background thread instead of the main thread (the block you provide in your example is executed on the main queue).

For completeness I'll also comment on the code you provided.

You're trying to set the frame of the node in the block when you're creating it, so you're trying to set the frame on it during its initialization. That causes your problem. I don't think you actually need to set the frame on the node when you're using initWithViewBlock: because internally ASDisplayNode uses the block to directly create its _view property which is added to the view hierarchy in the end.

I also noticed you're calling addSubview: from the background queue, you should always dispatch back to the main queue before you call that method. AsyncDisplayKit also adds addSubNode: to UIView for convenience.

I've changed you're code to reflect the changes though I recommend you use ASTextNode here.

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

这篇关于添加自定义按钮与AsyncDisplayKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆