![](/img/trans.png)
[英]Add a Pan Gesture Recognizer to an UITableView in Swift
[英]Add Gesture Recognizer to SKShapeNode Swift 4
我希望當用戶在SKShapeNode上向上滾動時執行一個函數。
我可以使用以下代碼針對整個視圖進行操作:
let swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedUp:"))
swipeUp.direction = .up
view?.addGestureRecognizer(swipeUp);
我如何修改此代碼以使其可用於SKShapeNode。 謝謝
通常,您不能將手勢識別器添加到SKScene
的實例,因為該類不是UIKit
一部分。 但是,由於已將手勢識別器添加到視圖中,因此可以使用單獨的處理程序方法來響應滑動手勢。
override func didMove(to view: SKView) {
let swipeUpGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(self.handleSwipeFrom))
self.view!.addGestureRecognizer(swipeUpGestureRecognizer)
}
@objc func handleSwipeFrom(_ recognizer: UISwipeGestureRecognizer) {
switch recognizer.state {
case .changed:
if recognizer.direction == .up {
let touchInView = recognizer.location(in: recognizer.view)
let touch = convertPoint(fromView: touchInView)
let nodeArray = nodes(at: touch)
for node in nodeArray {
if node.name == "my shape node" {
// You've got a reference to your shape node and can take some action here (add movement or whatever)
}
}
}
default:
return
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.