如何为所有屏幕的 iPhone 应用程序添加垂直滑动手势? [英] How to add a vertical swipe gesture to iPhone app for all screens?
问题描述
我想为我的应用添加一个手势,这样当用户垂直滑动时,它会触发一个方法来做某事.滑动可以是向上或向下.我从来没有用手势做过任何事情,所以这是我第一次使用手势,而不是 UITableView 中包含的用于删除行的手势.
I'd like to add a gesture to my app so when the user swipes vertically it triggers a method to do something. The swipe can be up or down. I've never done anything with gestures so this is my first use of a gesture other than what is included in a UITableView for deleting rows.
另一个问题是我的大部分屏幕都是 UITableView,所以用户可以简单地滚动 UITableView.所以我想知道我是否可以使用两指滑动(垂直)来检测运行代码的手势,而不是单指滑动来滚动 UITableView?
The other problem is that most of my screens are UITableViews so the user could be simply scrolling the UITableView. So I am wondering if I could use a two finger swipe (vertical) to detect the gesture to run the code vs. a single finger swipe to scroll the UITableView?
提前致谢.
尼尔
推荐答案
ApplicationDidLaunch 中的内容:
This goes in ApplicationDidLaunch:
UISwipeGestureRecognizer *swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreen:)] autorelease];
swipeGesture.numberOfTouchesRequired = 2;
swipeGesture.direction = (UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown);
[window addGestureRecognizer:swipeGesture];
然后执行
- (void) swipedScreen:(UISwipeGestureRecognizer*)swipeGesture {
// do stuff
}
使用 UIGestureRecognizer 和 UISwipeGestureRecognizer.
此外,如果您希望检测滑动的方向,则必须设置两个单独的手势识别器.您无法从滑动手势识别器获取滑动方向,只能获取其注册识别的方向.
Also if you wish to detect the direction of the swipe you will have to setup two separate gesture recognizers. You can not get the direction of a swipe from a swipe gesture recognizer, only the directions it is registered to recognize.
这篇关于如何为所有屏幕的 iPhone 应用程序添加垂直滑动手势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!