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

我已经解决了在touchedMoved中移动正确的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函数中执行相同的操作。在我的rotate函数中,它仅旋转第一个节点,而不管我要触摸哪个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
        }
    }

供参考,这是我如何定义touchedNode(在touchBegan中):
    touches: Set<UITouch>, with event: UIEvent?) {
    touch = touches.first!
    positionInScene = self.touch?.location(in: self)
    touchedNode = self.atPoint(positionInScene!)

最佳答案

UIGestureRecognizer具有location(in:UIView)方法。您可以使用self.view?.convert(sender.location(in: self.view), to: self)来获取UIRotationGestureRecognizer的位置,并使用类似于touchesBegan的逻辑。
convert(_:to:)将确保该点位于场景的坐标空间中。

关于ios - 如何在UIRotationRecognizer(SpriteKit + Swift 3.0)中找到被触摸的节点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40823897/

10-14 21:19
查看更多