如何根据用户的拖动使矩形围绕圆移动?

时间:2019-03-18 10:03:25

标签: swift xcode uibezierpath touchesbegan skshapenode

我想尝试根据用户拖动的位置来移动对象。此刻,我用以下代码将圆圈圈了起来:

What it looks like (image)

let rotateAction = SKAction.rotate(byAngle: 2.0 * CGFloat(M_PI), duration: 8.0)
container.run(SKAction.repeatForever(rotateAction))

这是我创建矩形的代码:

let path = UIBezierPath()

    path.move(to: CGPoint(x: 0, y: -200))

    path.addLine(to: CGPoint(x: 0, y: -160))

    path.addArc(withCenter: CGPoint.zero,
                radius: 160,
                startAngle: CGFloat(3.0 * M_PI_2),
                endAngle: CGFloat(0),
                clockwise: true)

    path.addLine(to: CGPoint(x: 200, y: 0))
    path.addArc(withCenter: CGPoint.zero,
                radius: 200,
                startAngle: CGFloat(0.0),
                endAngle: CGFloat(3.0 * M_PI_2),
                clockwise: false)

    let section = SKShapeNode(path: path.cgPath)
    section.position = CGPoint(x: 0, y: 0)
    section.fillColor = .white
    section.strokeColor = .white

    let section2 = SKShapeNode(path: path.cgPath)
    section2.position = CGPoint(x: 0, y: 0)
    section2.fillColor = .white
    section2.strokeColor = .white
    section2.zRotation = CGFloat(M_PI);

    container.addChild(section)
    container.addChild(section2)

    addChild(container)

当用户拖动时:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    let rotateAction = SKAction.rotate(byAngle: 2.0 * CGFloat(M_PI), duration: 8.0)
    container.run(SKAction.repeatForever(rotateAction))
}

我希望对此有所帮助。

谢谢!

0 个答案:

没有答案