Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode 如何修复UILongPressGestureRecognitor的代码?_Xcode_Swift - Fatal编程技术网

Xcode 如何修复UILongPressGestureRecognitor的代码?

Xcode 如何修复UILongPressGestureRecognitor的代码?,xcode,swift,Xcode,Swift,我希望我的英雄节点在左侧长按时向左移动,在右侧长按时向右移动,就像在这个游戏中一样。我有点不知道下一步要补充什么 func beginTouchRecognizer(){ let longPress = UILongPressGestureRecognizer(target: self, action: Selector("longPressTouch:")) longPress.minimumPressDuration = 1.0 self.view?.

我希望我的英雄节点在左侧长按时向左移动,在右侧长按时向右移动,就像在这个游戏中一样。我有点不知道下一步要补充什么

func beginTouchRecognizer(){
    let longPress = UILongPressGestureRecognizer(target: self,
        action: Selector("longPressTouch:"))
    longPress.minimumPressDuration = 1.0
    self.view?.addGestureRecognizer(longPress)
}
在longPressTouch:method中,您可以找到触摸的位置,并查看它是否位于用户正在点击的视图的特定区域内

例如,您可以这样做:

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
    }
}

未经测试的代码,但它应该可以让您继续使用。

我现在无法访问我的mac电脑,但我将在明天试用,并让您知道。谢谢你的帮助。我把这两个代码都添加到了didMoveToView中,当我运行它时,它就不工作了。我做错了什么?谢谢。如果你不忙,你能帮我一下吗?我还是想不出来?谢谢