Merge pull request #651 from rcancro/viewBlock

added a callback for initWithViewBlock/initWithLayerBlock
This commit is contained in:
appleguy 2015-09-15 18:50:24 +02:00
commit a9f2bda345
7 changed files with 81 additions and 27 deletions

View File

@ -31,13 +31,13 @@
return self; return self;
} }
- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock - (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock
{ {
ASDisplayNodeAssertNotSupported(); ASDisplayNodeAssertNotSupported();
return nil; return nil;
} }
- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock - (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock
{ {
ASDisplayNodeAssertNotSupported(); ASDisplayNodeAssertNotSupported();
return nil; return nil;

View File

@ -15,15 +15,23 @@
#import <AsyncDisplayKit/ASLayoutable.h> #import <AsyncDisplayKit/ASLayoutable.h>
@class ASDisplayNode;
/** /**
* UIView creation block. Used to create the backing view of a new display node. * UIView creation block. Used to create the backing view of a new display node.
*/ */
typedef UIView *(^ASDisplayNodeViewBlock)(); typedef UIView *(^ASDisplayNodeViewBlock)();
/** /**
* CALayer creation block. Used to create the backing layer of a new display node. * CALayer creation block. Used to create the backing layer of a new display node.
*/ */
typedef CALayer *(^ASDisplayNodeLayerBlock)(); typedef CALayer *(^ASDisplayNodeLayerBlock)();
/**
* ASDisplayNode loaded callback block. Used for any additional setup after node's layer/view has been loaded
*/
typedef void (^ASDisplayNodeDidLoadBlock)(ASDisplayNode *node);
/** /**
* An `ASDisplayNode` is an abstraction over `UIView` and `CALayer` that allows you to perform calculations about a view * An `ASDisplayNode` is an abstraction over `UIView` and `CALayer` that allows you to perform calculations about a view
* hierarchy off the main thread, and could do rendering off the main thread as well. * hierarchy off the main thread, and could do rendering off the main thread as well.
@ -65,6 +73,17 @@ typedef CALayer *(^ASDisplayNodeLayerBlock)();
*/ */
- (id)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock; - (id)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock;
/**
* @abstract Alternative initializer with a block to create the backing view.
*
* @param viewBlock The block that will be used to create the backing view.
* @param didLoadBlock The block that will be called after the view created by the viewBlock is loaded
*
* @return An ASDisplayNode instance that loads its view with the given block that is guaranteed to run on the main
* queue. The view will render synchronously and -layout and touch handling methods on the node will not be called.
*/
- (id)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock;
/** /**
* @abstract Alternative initializer with a block to create the backing layer. * @abstract Alternative initializer with a block to create the backing layer.
* *
@ -73,7 +92,18 @@ typedef CALayer *(^ASDisplayNodeLayerBlock)();
* @return An ASDisplayNode instance that loads its layer with the given block that is guaranteed to run on the main * @return An ASDisplayNode instance that loads its layer with the given block that is guaranteed to run on the main
* queue. The layer will render synchronously and -layout and touch handling methods on the node will not be called. * queue. The layer will render synchronously and -layout and touch handling methods on the node will not be called.
*/ */
- (id)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock; - (id)initWithLayerBlock:(ASDisplayNodeLayerBlock)layerBlock;
/**
* @abstract Alternative initializer with a block to create the backing layer.
*
* @param viewBlock The block that will be used to create the backing layer.
* @param didLoadBlock The block that will be called after the layer created by the layerBlock is loaded
*
* @return An ASDisplayNode instance that loads its layer with the given block that is guaranteed to run on the main
* queue. The layer will render synchronously and -layout and touch handling methods on the node will not be called.
*/
- (id)initWithLayerBlock:(ASDisplayNodeLayerBlock)layerBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock;
/** @name Properties */ /** @name Properties */

View File

@ -256,33 +256,47 @@ static ASDisplayNodeMethodOverrides GetASDisplayNodeMethodOverrides(Class c)
- (id)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock - (id)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock
{ {
if (!(self = [super init])) return [self initWithViewBlock:viewBlock didLoadBlock:nil];
return nil;
ASDisplayNodeAssertNotNil(viewBlock, @"should initialize with a valid block that returns a UIView");
[self _initializeInstance];
_viewBlock = viewBlock;
_flags.synchronous = YES;
return self;
} }
- (id)initWithLayerBlock:(ASDisplayNodeLayerBlock)layerBlock - (id)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock
{ {
if (!(self = [super init])) if (!(self = [super init]))
return nil; return nil;
ASDisplayNodeAssertNotNil(layerBlock, @"should initialize with a valid block that returns a CALayer"); ASDisplayNodeAssertNotNil(viewBlock, @"should initialize with a valid block that returns a UIView");
[self _initializeInstance]; [self _initializeInstance];
_layerBlock = layerBlock; _viewBlock = viewBlock;
_nodeLoadedBlock = didLoadBlock;
_flags.synchronous = YES; _flags.synchronous = YES;
_flags.layerBacked = YES;
return self; return self;
} }
- (id)initWithLayerBlock:(ASDisplayNodeLayerBlock)layerBlock
{
return [self initWithLayerBlock:layerBlock didLoadBlock:nil];
}
- (id)initWithLayerBlock:(ASDisplayNodeLayerBlock)layerBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock
{
if (!(self = [super init]))
return nil;
ASDisplayNodeAssertNotNil(layerBlock, @"should initialize with a valid block that returns a CALayer");
[self _initializeInstance];
_layerBlock = layerBlock;
_nodeLoadedBlock = didLoadBlock;
_flags.synchronous = YES;
_flags.layerBacked = YES;
return self;
}
- (void)dealloc - (void)dealloc
{ {
ASDisplayNodeAssertMainThread(); ASDisplayNodeAssertMainThread();
@ -424,7 +438,7 @@ static ASDisplayNodeMethodOverrides GetASDisplayNodeMethodOverrides(Class c)
} }
{ {
TIME_SCOPED(_debugTimeForDidLoad); TIME_SCOPED(_debugTimeForDidLoad);
[self didLoad]; [self __didLoad];
} }
if (self.placeholderEnabled) { if (self.placeholderEnabled) {
@ -1482,6 +1496,15 @@ static NSInteger incrementIfFound(NSInteger i) {
_flags.isMeasured = NO; _flags.isMeasured = NO;
} }
- (void)__didLoad
{
if (_nodeLoadedBlock) {
_nodeLoadedBlock(self);
_nodeLoadedBlock = nil;
}
[self didLoad];
}
- (void)didLoad - (void)didLoad
{ {
ASDisplayNodeAssertMainThread(); ASDisplayNodeAssertMainThread();

View File

@ -86,13 +86,13 @@
return self; return self;
} }
- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock - (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock
{ {
ASDisplayNodeAssertNotSupported(); ASDisplayNodeAssertNotSupported();
return nil; return nil;
} }
- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock - (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock
{ {
ASDisplayNodeAssertNotSupported(); ASDisplayNodeAssertNotSupported();
return nil; return nil;

View File

@ -96,13 +96,13 @@
return self; return self;
} }
- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock - (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock
{ {
ASDisplayNodeAssertNotSupported(); ASDisplayNodeAssertNotSupported();
return nil; return nil;
} }
- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock - (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock
{ {
ASDisplayNodeAssertNotSupported(); ASDisplayNodeAssertNotSupported();
return nil; return nil;

View File

@ -149,13 +149,13 @@ ASDISPLAYNODE_INLINE CGFloat ceilPixelValue(CGFloat f)
return self; return self;
} }
- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock - (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock
{ {
ASDisplayNodeAssertNotSupported(); ASDisplayNodeAssertNotSupported();
return nil; return nil;
} }
- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock - (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock
{ {
ASDisplayNodeAssertNotSupported(); ASDisplayNodeAssertNotSupported();
return nil; return nil;

View File

@ -60,6 +60,7 @@ typedef NS_OPTIONS(NSUInteger, ASDisplayNodeMethodOverrides) {
ASDisplayNodeViewBlock _viewBlock; ASDisplayNodeViewBlock _viewBlock;
ASDisplayNodeLayerBlock _layerBlock; ASDisplayNodeLayerBlock _layerBlock;
ASDisplayNodeDidLoadBlock _nodeLoadedBlock;
Class _viewClass; Class _viewClass;
Class _layerClass; Class _layerClass;
UIView *_view; UIView *_view;