如何绘制两种不同颜色的空心圆?
How to draw a hollow circle with two different colors?
我非常熟悉绘制填充的 createEllipseInRect 形状节点,但我想知道是否有办法以编程方式执行此操作。我想要一个从中心分裂的圆圈,两边都有两种不同的颜色。我没有代码,因为我不知道从哪里开始。
非常感谢帮助。
您可以尝试使用 SKCropNode,这样您可以只显示每个圆的一半。有关此示例,请参见下面的代码。
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
anchorPoint = CGPointMake(0.5, 0.5)
// Half Circle #1
let myCrop1 = SKCropNode()
let myMask1 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(100, 100))
myMask1.position.y = -50
let circle1 = SKShapeNode(circleOfRadius: 50)
circle1.lineWidth = 0
circle1.fillColor = UIColor.blueColor()
myCrop1.addChild(circle1)
myCrop1.maskNode = myMask1
addChild(myCrop1)
// Half Circle #2
let myCrop2 = SKCropNode()
let myMask2 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(100, 100))
myMask2.position.y = 50
let circle2 = SKShapeNode(circleOfRadius: 50)
circle2.lineWidth = 0
circle2.fillColor = UIColor.redColor()
myCrop2.addChild(circle2)
myCrop2.maskNode = myMask2
addChild(myCrop2)
}
}
我之前并没有真正使用过 SKCropNode,所以我不确定我的代码有多好,但下面是我在 iPhone.
上得到的结果
编辑:如果需要,您应该能够添加第三个 SKCropNode 以使圆心透明。
编辑:下方为透明中心
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
anchorPoint = CGPointMake(0.5, 0.5)
let transparentCenterMask = SKShapeNode(circleOfRadius: 50)
transparentCenterMask.lineWidth = 20
let transparentCenterCrop = SKCropNode()
transparentCenterCrop.maskNode = transparentCenterMask
// Half Circle #1
let myCrop1 = SKCropNode()
let myMask1 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(100, 100))
myMask1.position.y = -50
let circle1 = SKShapeNode(circleOfRadius: 50)
circle1.lineWidth = 0
circle1.fillColor = UIColor.blueColor()
myCrop1.addChild(circle1)
myCrop1.maskNode = myMask1
transparentCenterCrop.addChild(myCrop1)
// Half Circle #2
let myCrop2 = SKCropNode()
let myMask2 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(100, 100))
myMask2.position.y = 50
let circle2 = SKShapeNode(circleOfRadius: 50)
circle2.lineWidth = 0
circle2.fillColor = UIColor.redColor()
myCrop2.addChild(circle2)
myCrop2.maskNode = myMask2
transparentCenterCrop.addChild(myCrop2)
addChild(transparentCenterCrop)
}
}
我非常熟悉绘制填充的 createEllipseInRect 形状节点,但我想知道是否有办法以编程方式执行此操作。我想要一个从中心分裂的圆圈,两边都有两种不同的颜色。我没有代码,因为我不知道从哪里开始。
非常感谢帮助。
您可以尝试使用 SKCropNode,这样您可以只显示每个圆的一半。有关此示例,请参见下面的代码。
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
anchorPoint = CGPointMake(0.5, 0.5)
// Half Circle #1
let myCrop1 = SKCropNode()
let myMask1 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(100, 100))
myMask1.position.y = -50
let circle1 = SKShapeNode(circleOfRadius: 50)
circle1.lineWidth = 0
circle1.fillColor = UIColor.blueColor()
myCrop1.addChild(circle1)
myCrop1.maskNode = myMask1
addChild(myCrop1)
// Half Circle #2
let myCrop2 = SKCropNode()
let myMask2 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(100, 100))
myMask2.position.y = 50
let circle2 = SKShapeNode(circleOfRadius: 50)
circle2.lineWidth = 0
circle2.fillColor = UIColor.redColor()
myCrop2.addChild(circle2)
myCrop2.maskNode = myMask2
addChild(myCrop2)
}
}
我之前并没有真正使用过 SKCropNode,所以我不确定我的代码有多好,但下面是我在 iPhone.
上得到的结果编辑:如果需要,您应该能够添加第三个 SKCropNode 以使圆心透明。
编辑:下方为透明中心
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
anchorPoint = CGPointMake(0.5, 0.5)
let transparentCenterMask = SKShapeNode(circleOfRadius: 50)
transparentCenterMask.lineWidth = 20
let transparentCenterCrop = SKCropNode()
transparentCenterCrop.maskNode = transparentCenterMask
// Half Circle #1
let myCrop1 = SKCropNode()
let myMask1 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(100, 100))
myMask1.position.y = -50
let circle1 = SKShapeNode(circleOfRadius: 50)
circle1.lineWidth = 0
circle1.fillColor = UIColor.blueColor()
myCrop1.addChild(circle1)
myCrop1.maskNode = myMask1
transparentCenterCrop.addChild(myCrop1)
// Half Circle #2
let myCrop2 = SKCropNode()
let myMask2 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(100, 100))
myMask2.position.y = 50
let circle2 = SKShapeNode(circleOfRadius: 50)
circle2.lineWidth = 0
circle2.fillColor = UIColor.redColor()
myCrop2.addChild(circle2)
myCrop2.maskNode = myMask2
transparentCenterCrop.addChild(myCrop2)
addChild(transparentCenterCrop)
}
}