我试图弄清楚如何检测 shapeNode 是否已被按下,特别是使用 LongPress Gesture。请看下面的代码。知道出了什么问题,为什么我看不到“找到!” 信息?
class Test: SKScene {
let longPressGesture = UILongPressGestureRecognizer()
override func didMove(to view: SKView) {
longPressGesture.addTarget(self, action: #selector(GameScene.longPress))
self.view?.addGestureRecognizer(longPressGesture)
let testPath = UIBezierPath(rect: CGRect(x: (view.scene?.frame.midX)!, y: (view.scene?.frame.midY)!, width: 2 * 50.0, height: 2 * 50.0)).cgPath
let testNode = Node.init(path: testPath, nodeName: "TEST")
testNode.fillColor = UIColor.brown
testNode.position = CGPoint(x: (view.scene?.frame.midX)!, y: (view.scene?.frame.midY)!)
self.addChild(testNode)
}
func longPress(_ sender: UILongPressGestureRecognizer) {
let longPressLocation = sender.location(in: self.view)
if sender.state == .began {
for child in self.children {
if let shapeNode = child as? SKShapeNode {
if shapeNode.contains(longPressLocation) {
print("Found!")
}
}
}
} else if sender.state == .ended {
print("ended")
}
}
}
非常感谢您的帮助!