在我的 iPhone 应用中,我需要识别用户在视图上进行的滑动手势。
我希望能够识别滑动手势并在滑动时执行一个功能。
我需要视图在用户进行滑动手势时水平滑动并显示另一个视图。
需要做什么?
如何进行识别?
在我的 iPhone 应用中,我需要识别用户在视图上进行的滑动手势。
我希望能够识别滑动手势并在滑动时执行一个功能。
我需要视图在用户进行滑动手势时水平滑动并显示另一个视图。
需要做什么?
如何进行识别?
如果您知道它如何工作,但仍需要一个快速示例,则在此处!(至少在我需要复制粘贴示例而无需尝试记住它时,它将变得方便)
UISwipeGestureRecognizer *mSwipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(doSomething)];
[mSwipeUpRecognizer setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:mSwipeUpRecognizer];
并且在 .h 文件中添加:
<UIGestureRecognizerDelegate>
UISwipeGestureRecognizer
。手势识别很容易,没有什么需要解释的。甚至还有关于此主题的WWDC10视频,其中包括120和121会话。 :)UISwipeGestureRecognizer 教程(检测 iPhone 上的滑动手势)
下面是一个代码示例,用于在Swift中实现该功能:UISwipeGestureRecognizer
。这有点奇怪,因为UISwipeGestureRecognizer.direction
属性是一个选项样式的位掩码,但每个识别器只能处理一个方向。如果您希望可以将它们全部发送到同一个处理程序,并在那里进行排序,或者将它们发送到不同的处理程序。这是一个实现示例:override func viewDidLoad() {
super.viewDidLoad()
var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)
var swipeDown = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeDown.direction = UISwipeGestureRecognizerDirection.Down
self.view.addGestureRecognizer(swipeDown)
}
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
println("Swiped right")
case UISwipeGestureRecognizerDirection.Down:
println("Swiped down")
default:
break
}
}
}
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGestureRight))
swipeRight.direction = UISwipeGestureRecognizer.Direction.right
self.view.addGestureRecognizer(swipeRight)