gpt4 book ai didi

iOS 子类化 SKShapeNode

转载 作者:行者123 更新时间:2023-11-29 10:27:20 29 4
gpt4 key购买 nike

如何对没有实例方法初始化器的 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;
}

在这种情况下,selfSKShapeNode 而不是 CAButtonNode 的实例。

谢谢。

最佳答案

您可以向 SKShapeNode 添加类扩展

extension SKShapeNode
{
class func nodeWithColor( color:UIColor, radius:Float ) -> Self
{
let node = SKShapeNode()
node.path = CGPathCreateWithEllipseInRect(...)
node.fillColor = ...
node.strokeColor = ...

return node
}
}

像这样使用:

let newNode = SKShapeNode.nodeWithColor( <i>theColor</i>, radius: <i>theRadius</i> )

或者,在 Obj-C 中:

@implementation SKShapeNode (NodeWithColorAndRadius)

+(instancetype)nodeWithColor:(UIColor*)color radius:(CGFloat)r
{
SKShapeNode * result = [ self new ] ;
result.path = ...;
result.fillColor = ...;
result.strokeColor = ...;
}

@end

这样调用:

[ SKShapeNode nodeWithColor:<i>theColor</i> radius:<i>theRadius</i> ]

关于iOS 子类化 SKShapeNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31494317/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com