我正在开发IOS应用程序。我使用Facebook AsyncDisplayKit库。我想要一个ASNodeCell Bu中的按钮,当块捕获时,我得到了“变量'node'未初始化。如何在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];
});
请注意,在您的情况下,无需使用UIButton,您可以将ASTextNode用作按钮,因为它是从ASControlNode继承的(对于ASImageNode也是如此)。在指南第一页的底部对此进行了描述:http : //asyncdisplaykit.org/guide/。这也将允许您在后台线程而不是主线程上进行文本大小调整(示例中提供的块在主队列上执行)。
为了完整起见,我还将评论您提供的代码。
您正在尝试在创建块时设置该节点的框架,因此您正在尝试在其初始化期间在其上设置该框架。那会导致您的问题。我认为使用initWithViewBlock时实际上不需要在节点上设置框架:因为ASDisplayNode在内部使用该块直接创建其_view属性,该属性最终添加到视图层次结构中。
我还注意到您正在从后台队列中调用addSubview:在调用该方法之前,应始终分派回主队列。为了方便起见,AsyncDisplayKit还向UIView添加addSubNode:。
尽管我建议您在此处使用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];
);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句