How to draw a hollow circle with two different colors?

我们两清 提交于 2019-12-24 00:30:55

问题


Im very familiar with drawing a filled createEllipseInRect shape node, but I was wondering if there's a way to do this programmatically. I want a circle thats split through the center with two separate colors on either side. I don't have code since I have no idea where to start with this.

something like this

Help much appreciated.


回答1:


You can try using SKCropNode, this allows you to show only half of each circle. See below code for example of this.

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)


    }

}

I haven't really used SKCropNode that much before, so I'm not sure how good my code is, but below is the result I got on my iPhone.

EDIT: You should be able to add a 3rd SKCropNode to make the centre of the circle transparent if required.

EDIT: Below for transparent centre

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)


    }

}



来源:https://stackoverflow.com/questions/35109905/how-to-draw-a-hollow-circle-with-two-different-colors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!