Add read-only names to UIView and CALayer

- Backed by name of the underlying ASDisplayNode.
- Remove goofy usages of `setValue:forKey:` in ASDisplayNodeTests and ASDisplayNodeAppearanceTests.
This commit is contained in:
Huy Nguyen
2015-10-26 07:33:11 +02:00
parent fd0b3aaf52
commit 53a3b76d6d
4 changed files with 35 additions and 12 deletions

View File

@@ -650,6 +650,7 @@ typedef void (^ASDisplayNodeDidLoadBlock)(ASDisplayNode *node);
* @param node The node to be added.
*/
- (void)addSubnode:(ASDisplayNode *)node;
- (NSString *)name;
@end
/** CALayer(AsyncDisplayKit) defines convenience method for adding sub-ASDisplayNode to a CALayer. */
@@ -660,6 +661,7 @@ typedef void (^ASDisplayNodeDidLoadBlock)(ASDisplayNode *node);
* @param node The node to be added.
*/
- (void)addSubnode:(ASDisplayNode *)node;
- (NSString *)name;
@end
@interface ASDisplayNode (Deprecated)

View File

@@ -2203,6 +2203,11 @@ static const char *ASDisplayNodeAssociatedNodeKey = "ASAssociatedNode";
}
}
- (NSString *)name
{
return self.asyncdisplaykit_node.name;
}
@end
@implementation CALayer (AsyncDisplayKit)
@@ -2212,6 +2217,11 @@ static const char *ASDisplayNodeAssociatedNodeKey = "ASAssociatedNode";
[self addSublayer:node.layer];
}
- (NSString *)name
{
return self.asyncdisplaykit_node.name;
}
@end

View File

@@ -56,12 +56,15 @@ static dispatch_block_t modifyMethodByAddingPrologueBlockAndReturnCleanupBlock(C
@interface ASDisplayNodeAppearanceTests : XCTestCase
@end
// Conveniences for making nodes named a certain way
#define DeclareNodeNamed(n) ASDisplayNode *n = [[ASDisplayNode alloc] init]; n.name = @#n
#define DeclareViewNamed(v) UIView *v = viewWithName(@#v)
// FIXME: It's goofy to use `setValue:forKey:` here but importing ASDisplayNodeInternal.h
// in this file causes build to fail (compiler chokes at ASDN::RecursiveMutex)
#define DeclareViewNamed(v) UIView *v = [[UIView alloc] init]; [v.layer setValue:@#v forKey:@"asyncdisplaykit_node.name"]
#define DeclareLayerNamed(l) CALayer *l = [[CALayer alloc] init]; [l setValue:@#l forKey:@"asyncdisplaykit_node.name"]
static UIView *viewWithName(NSString *name) {
ASDisplayNode *n = [[ASDisplayNode alloc] init];
n.name = name;
return n.view;
}
@implementation ASDisplayNodeAppearanceTests
{

View File

@@ -17,20 +17,29 @@
#import "UIView+ASConvenience.h"
// Conveniences for making nodes named a certain way
#define DeclareNodeNamed(n) ASDisplayNode *n = [[ASDisplayNode alloc] init]; n.name = @#n
#define DeclareViewNamed(v) UIView *v = viewWithName(@#v)
#define DeclareLayerNamed(l) CALayer *l = layerWithName(@#l)
// FIXME: It's goofy to use `setValue:forKey:` here but importing ASDisplayNodeInternal.h
// in this file causes build to fail (compiler chokes at ASDN::RecursiveMutex)
#define DeclareViewNamed(v) UIView *v = [[UIView alloc] init]; [v.layer setValue:@#v forKey:@"asyncdisplaykit_node.name"]
#define DeclareLayerNamed(l) CALayer *l = [[CALayer alloc] init]; [l setValue:@#l forKey:@"asyncdisplaykit_node.name"]
static UIView *viewWithName(NSString *name) {
ASDisplayNode *n = [[ASDisplayNode alloc] init];
n.name = name;
return n.view;
}
static CALayer *layerWithName(NSString *name) {
ASDisplayNode *n = [[ASDisplayNode alloc] init];
n.layerBacked = YES;
n.name = name;
return n.layer;
}
static NSString *orderStringFromSublayers(CALayer *l) {
return [[l.sublayers valueForKey:@"asyncdisplaykit_node.name"] componentsJoinedByString:@","];
return [[l.sublayers valueForKey:@"name"] componentsJoinedByString:@","];
}
static NSString *orderStringFromSubviews(UIView *v) {
return [[v.subviews valueForKeyPath:@"layer.asyncdisplaykit_node.name"] componentsJoinedByString:@","];
return [[v.subviews valueForKey:@"name"] componentsJoinedByString:@","];
}
static NSString *orderStringFromSubnodes(ASDisplayNode *n) {
@@ -1377,7 +1386,6 @@ static inline BOOL _CGPointEqualToPointWithEpsilon(CGPoint point1, CGPoint point
DeclareNodeNamed(b);
DeclareNodeNamed(c);
DeclareViewNamed(d);
DeclareLayerNamed(e);
[parent layer];