Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在WatchOS3中向SKShapeNode添加手势_Ios_Swift_Sprite Kit_Watchkit - Fatal编程技术网

Ios 在WatchOS3中向SKShapeNode添加手势

Ios 在WatchOS3中向SKShapeNode添加手势,ios,swift,sprite-kit,watchkit,Ios,Swift,Sprite Kit,Watchkit,我可以在SpriteKit场景中添加手势。但无法将手势添加到场景中的某个节点 另外,touchsbegind方法在WatchKit中不可用。因此,在我的界面中添加了手势识别器,并在SpriteKit场景中传递它 但这会导致将panGesture添加到整个场景,而不是我的节点 有没有办法只向我的一个节点添加手势?您永远不会向节点添加手势 您只能向任何可以识别手势的对象添加手势 在UIKit中,这些是UIView 在Watchkit中,这些是接口 现在,您要在Watchkit中执行的操作是,当手势开

我可以在
SpriteKit
场景中添加手势。但无法将手势添加到场景中的某个节点

另外,
touchsbegind
方法在
WatchKit
中不可用。因此,在我的界面中添加了手势识别器,并在SpriteKit场景中传递它

但这会导致将panGesture添加到整个场景,而不是我的节点


有没有办法只向我的一个节点添加手势?

您永远不会向节点添加手势

您只能向任何可以识别手势的对象添加手势

在UIKit中,这些是UIView

在Watchkit中,这些是接口

现在,您要在Watchkit中执行的操作是,当手势开始时,检测您是否正在触摸节点

可以在Watchkit中执行此操作,方法是使用
locationinObject
获取位置,并通过执行以下操作将其转换为场景坐标系:

extension WKGestureRecognizer {
    var position : CGPoint
    {
        get
        {
            let location = locationInObject()
            let bounds = objectBounds()
            let dWidth = self.scene.size.width / bounds.size.width
            let dHeight = self.scene.size.height / bounds.size.height
            let x = (location.x * dWidth) - scene.width * scene.anchorPoint.x)
            let y = (bounds.maxY - location.y) * dHeight - (scene.height * scene.anchorPoint.y)
            return CGPoint(x:x, y:y)
        }
    }
}
这样做的目的是获取您在界面中的位置,然后通过翻转y轴将其转换为场景,然后调整场景宽度与界面宽度的差异

现在要使用它,只要在识别器方法中调用它,并检查您正在触摸的节点是否是您的节点

func didPan(_ recognizer: WKPanGestureRecognizer) {
        switch(recognizer.state)
        {
            case .began:
                let scenePosition = recognizer.position
                let node = scene.atPoint(scenePosition)
                guard let node = shapeNode else {return}
                //allow pan, so make a note or something
            case //do other cases here
        }
}