mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-08-31 18:02:20 +00:00
This adds new initializer methods to ASDisplayNode: ```objc initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock initWithLayerBlock:(ASDisplayNodeLayerBlock)layerBlock ``` Sometimes a view can't be constructed with `-[initWithViewClass:]` but you want to use it with ASDK, so these new methods provide a way to wrap an existing view in a node. The API is meant to preserve ASDisplayNode's behavior, so you can still construct and set properties on the node on a background queue before its view is loaded; even though the view was created a priori, it is not considered to be loaded until `node.view` is accessed. Using the API looks like this: dispatch_async(backgroundQueue, ^{ ASDisplayNode *node = [ASDisplayNode alloc] initWithViewBlock:^{ // Guaranteed to run on the main queue UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button sizeToFit]; node.frame = button.frame; return button; }]; // Use `node` as you normally would... node.backgroundColor = [UIColor redColor]; }); The main thing this bridging API doesn't do (can't do?) is layout. Methods like `-[ASDisplayNode calculateSizeThatFits:]` and `-[ASDisplayNode layout]` cannot delegate to `[UIView sizeThatFits:]` and `[UIView layoutSubviews]` since the UIView methods must run on the main thread. If ASDK were internally asynchronous and could dispatch its layout methods to different threads (sort of like how ASTableView computes its cells' layouts) then we could mark nodes with externally provided views/layers as having "main-queue affinity" and delegate its layout to UIKit. Test cases are included and all existing tests pass.