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

时间:2015-04-09 03:15:31

标签: ios swift sprite-kit uibezierpath

我使用以下代码设置了一个包含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,它不起作用。我在StackOverflow上搜索了一些其他帖子,也没有运气让它工作。 任何想法我怎么能让它工作? 非常感谢。

1 个答案:

答案 0 :(得分:0)

您可以将SKShapeNodes添加到SKNode,然后轮播SKNode。例如,您可以像这样创建SKNode的自定义类

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)