如何在UIRotationRecognizer中找到触摸的节点(SpriteKit + Swift 3.0)

时间:2016-11-26 22:42:20

标签: ios swift sprite-kit swift3

我在屏幕上有三个相同类lineBlock的节点MovableBlock。我想旋转某人在屏幕上触摸的lineBlock节点。

我已经解决了这个问题,因为在touchMoved中移动了正确的lineBlock节点:

   override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        touch = touches.first!
        positionInScene = self.touch?.location(in: self)
        let previousPosition = self.touch?.previousLocation(in: self)
        let translation = CGVector(dx: (positionInScene?.x)! - (previousPosition?.x)!, dy: (positionInScene?.y)! - (previousPosition?.y)!)

        if touchedNode?.name?.contains("LineBlock") == true {
                (touchedNode as! MovableBlock).selected = true
                (touchedNode as! MovableBlock).parent!.parent!.run(SKAction.move(by: translation, duration: 0.0))
        }
    }

但我在UIRotationRecognizer函数中无法做到这一点。在我的旋转函数中,它只是旋转第一个节点,无论我触摸哪个lineBlock(类MovableBlock):

func rotate(_ sender: UIRotationGestureRecognizer){
        if lineBlock.selected == true {
                lineBlock.run(SKAction.rotate(byAngle: (-(self.rotationRecognizer?.rotation)!*2), duration: 0.0))
                rotationRecognizer?.rotation = 0
        }
    }

供参考,以下是我定义的touchNode(在touchBegan中):

    touches: Set<UITouch>, with event: UIEvent?) {
    touch = touches.first!
    positionInScene = self.touch?.location(in: self)
    touchedNode = self.atPoint(positionInScene!)

1 个答案:

答案 0 :(得分:1)

UIGestureRecognizer有一个location(in:UIView)方法。您可以使用self.view?.convert(sender.location(in: self.view), to: self)获取UIRotationGestureRecognizer的位置,并使用与touchesBegan中类似的逻辑。

convert(_:to:)将确保该点位于场景的坐标空间中。