使用AsyncDisplayKit添加自定义按钮
问题描述:
我正在开发一个IOS应用程序。我使用Facebook AsyncDisplayKit库。我想一个按钮ASNodeCell卜我“变量‘节点’当块中捕获的初始化。我如何添加的UIButton或ASNodeCell UIWebView的控制。请帮我使用AsyncDisplayKit添加自定义按钮
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还添加了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; <-- 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];
);
});
https://github.com/liaogang/ASPhotoBrowser.git,这个演示可能有帮助 – liaogang 2016-01-08 07:45:49