UIBezierPath 围绕圆圈内的一个点旋转,swift 和 SpriteKit

UIBezierPath rotate around a point within the circle itself, swift and SpriteKit

我使用以下代码设置了一个有 4 个偶数扇区的圆:

func centerCircle() {

    let center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
    // node1
    let node1bezierPath = UIBezierPath()
    node1bezierPath.addArcWithCenter(center, radius: 100, startAngle: 0.78, endAngle: 2.35, clockwise: true)
    node1bezierPath.addLineToPoint(center)

    let node1 = SKShapeNode(path: node1bezierPath.CGPath)
    node1.strokeColor = SKColor.redColor()
    node1.fillColor = SKColor.redColor()
    self.addChild(node1)
    // node2
    let node2bezierPath = UIBezierPath()
    node2bezierPath.addArcWithCenter(center, radius: 100, startAngle: 2.35, endAngle: 3.92, clockwise: true)
    node2bezierPath.addLineToPoint(center)

    let node2 = SKShapeNode(path: node2bezierPath.CGPath)
    node2.strokeColor = SKColor.blueColor()
    node2.fillColor = SKColor.blueColor()
    self.addChild(node2)
    // node3
    let node3bezierPath = UIBezierPath()
    node3bezierPath.addArcWithCenter(center, radius: 100, startAngle: 3.92, endAngle: 5.48, clockwise: true)
    node3bezierPath.addLineToPoint(center)

    let node3 = SKShapeNode(path: node3bezierPath.CGPath)
    node3.strokeColor = SKColor.greenColor()
    node3.fillColor = SKColor.greenColor()
    self.addChild(node3)
    // node4
    let node4bezierPath = UIBezierPath()
    node4bezierPath.addArcWithCenter(center, radius: 100, startAngle: 5.48, endAngle: 0.78, clockwise: true)
    node4bezierPath.addLineToPoint(center)

    let node4 = SKShapeNode(path: node4bezierPath.CGPath)
    node4.strokeColor = SKColor.yellowColor()
    node4.fillColor = SKColor.yellowColor()
    self.addChild(node4)

}

我想要实现的是,当我点击屏幕时,这个圆会围绕它的中心旋转 90 度。

center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))

我尝试设置 centerCircle() -> SKSpriteNode,并添加一个 SKAction.rotateByAngle,它不起作用。我在 Whosebug 上搜索了一些其他帖子,但也没有成功。 任何想法如何让它工作? 非常感谢。

您可以将 SKShapeNodes 添加到 SKNode,然后旋转 SKNode。例如,您可以像这样

创建 SKNode 的自定义 class
class FourColorCircle : SKNode {

    override init() {
        super.init()
        self.createCircle()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    func createCircle () {
        let center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
        // node1
        let node1bezierPath = UIBezierPath()
        node1bezierPath.addArcWithCenter(center, radius: 100, startAngle: 0.78, endAngle: 2.35, clockwise: true)
        node1bezierPath.addLineToPoint(center)

        let node1 = SKShapeNode(path: node1bezierPath.CGPath)
        node1.strokeColor = SKColor.redColor()
        node1.fillColor = SKColor.redColor()
        self.addChild(node1)
        // node2
        let node2bezierPath = UIBezierPath()
        node2bezierPath.addArcWithCenter(center, radius: 100, startAngle: 2.35, endAngle: 3.92, clockwise: true)
        node2bezierPath.addLineToPoint(center)

        let node2 = SKShapeNode(path: node2bezierPath.CGPath)
        node2.strokeColor = SKColor.blueColor()
        node2.fillColor = SKColor.blueColor()
        self.addChild(node2)
        // node3
        let node3bezierPath = UIBezierPath()
        node3bezierPath.addArcWithCenter(center, radius: 100, startAngle: 3.92, endAngle: 5.48, clockwise: true)
        node3bezierPath.addLineToPoint(center)

        let node3 = SKShapeNode(path: node3bezierPath.CGPath)
        node3.strokeColor = SKColor.greenColor()
        node3.fillColor = SKColor.greenColor()
        self.addChild(node3)
        // node4
        let node4bezierPath = UIBezierPath()
        node4bezierPath.addArcWithCenter(center, radius: 100, startAngle: 5.48, endAngle: 0.78, clockwise: true)
        node4bezierPath.addLineToPoint(center)

        let node4 = SKShapeNode(path: node4bezierPath.CGPath)
        node4.strokeColor = SKColor.yellowColor()
        node4.fillColor = SKColor.yellowColor()
        self.addChild(node4)

    }

    func rotate(angle : CGFloat, animated : Bool) {
        var rotateAction : SKAction!

        if animated {
            rotateAction = SKAction.rotateByAngle(angle, duration: 0.6)
        }
        else {
            rotateAction = SKAction.rotateByAngle(angle, duration: 0)
        }

        self.runAction(rotateAction)
    }
}

那你就可以这样用了,

let circle = FourColorCircle()
addChild(circle)
circle.position = CGPointMake(200, 200)
circle.rotate(3.14/2, animated: true)