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中,当我运行它时,它就不工作了。我做错了什么?谢谢。如果你不忙,你能帮我一下吗?我还是想不出来?谢谢