![](/img/trans.png)
[英]How would I use the UILongPressGestureRecognizer in my App in Swift Spritekit?
[英]How do I fix my code for the UILongPressGestureRecognizer?
我希望我的英雄節點在左側長按時移動到左側,而在右側長按時移動到右側,例如在此游戲中https://itunes.apple.com/us/app/運行鳥運行/ id951346475?mt = 8 。 我對接下來要添加的內容有些迷失。
func beginTouchRecognizer(){
let longPress = UILongPressGestureRecognizer(target: self,
action: Selector("longPressTouch:"))
longPress.minimumPressDuration = 1.0
self.view?.addGestureRecognizer(longPress)
}
在longPressTouch:
方法中,您可以找到觸摸的位置,並查看它是否在用戶點擊的視圖的特定區域內。
例如,您可以執行以下操作:
let WidthOfTapArea: CGFloat = 100
func longPressTouch(recognizer: UILongPressGestureRecognizer) {
let location = recognizer.locationInView(self.view)
if location.x < WidthOfTapArea {
// Handle a tap on the left side of the view
} else if location.x > (self.view.frame.width - WidthOfTapArea) {
// Handle a tap on the right side of the view
}
}
未經測試的代碼,但這應該可以幫助您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.