如何为所有屏幕的iPhone应用添加垂直滑动手势? [英] How to add a vertical swipe gesture to iPhone app for all screens?

查看:166
本文介绍了如何为所有屏幕的iPhone应用添加垂直滑动手势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用中添加一个手势,因此当用户垂直滑动时会触发一种方法来执行某些操作。滑动可以向上或向下。我从来没有做任何手势,所以这是我第一次使用手势而不是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.

另一个问题是大多数我的屏幕是UITableViews所以用户可以简单地滚动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?

谢谢你事先。

Neal

推荐答案

这是在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆