2015-03-03 131 views
2

我正在開發一個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]; 
}); 

enter image description here

+0

https://github.com/liaogang/ASPhotoBrowser.git,這個演示可能有幫助 – liaogang 2016-01-08 07:45:49

回答

4

注意在你的情況下不需要使用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]; 
); 
});