我想弄明白如何檢測shapeNode是否被按下,特別是與LongPress手勢。請看下面的代碼。任何想法有什麼不對,爲什麼我看不到「找到!」信息?UILongPressGestureRecognizer與SpriteKit
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")
}
}
}
非常感謝您的幫助!
我可能是錯的,但是你在做'view.scene?.frame'時,你可以只做'.frame' :) – Fluidity