我可以在SpriteKit
场景中添加手势。但是无法在场景中的某个节点上添加手势。
此外,touchesBegan
中未提供WatchKit
方法。所以在我的界面中添加了gestureRecognizer并将其传递给SpriteKit场景。
但这导致将panGesture添加到整个场景而不是我的节点。
有没有办法只将手势添加到我的某个节点?
答案 0 :(得分:1)
您不会向节点添加手势。
您只能向可以识别手势的任何内容添加手势。
在UIKit中,这些是UIViews
在Watchkit中,这些是WKInterfaces
现在你想在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
}
}