如何对没有实例方法初始化器的SKShapeNode进行子类化?我唯一能想到的就是:
+ (id)withColor:(UIColor *)aColor radius:(CGFloat)aRadius {
return [[self alloc] initWithColor:aColor radius: aRadius];
}
- (id)initWithColor:(UIColor *)aColor radius:(CGFloat)aRadius {
self = (CAButtonNode *)[SKShapeNode shapeNodeWithCircleOfRadius:aRadius];
self.fillColor = aColor;
self.strokeColor = [UIColor clearColor];
return self;
}在这种情况下,self是SKShapeNode而不是CAButtonNode的实例。
谢谢。
发布于 2015-07-18 18:54:29
如果你问我,我不确定这是否是正确的解决方案,这是非常不方便的,但它确实有效。我把这个留给任何可能遇到类似问题的人。
您需要做的是像通常一样调用[super init],然后手动设置节点的路径。下面的内容基本上模仿了SKShapeNode类方法shapeNodeWithCircleOfRadius:
- (id)initWithColor:(UIColor *)aColor radius:(CGFloat)aRadius {
if (self = [super init]) {
self.path = CGPathCreateWithEllipseInRect(CGRectMake(-aRadius, -aRadius, aRadius * 2, aRadius * 2), NULL);
self.fillColor = aColor;
self.strokeColor = [UIColor clearColor];
}
return self;
}如果有人知道,我很好奇他们为什么不像在大多数其他类中那样包括实例方法初始化器。
干杯!
https://stackoverflow.com/questions/31494317
复制相似问题