如何为所有屏幕的 iPhone 应用程序添加垂直滑动手势?

发布于 2024-10-11 06:20:53 字数 262 浏览 9 评论 0原文

我想向我的应用程序添加一个手势,以便当用户垂直滑动时它会触发一个方法来执行某些操作。滑动可以向上或向下。我从来没有用手势做过任何事情,所以这是我第一次使用手势,而不是 UITableView 中包含的用于删除行的手势。

另一个问题是我的大多数屏幕都是 UITableView,因此用户可以简单地滚动 UITableView。所以我想知道是否可以使用两根手指滑动(垂直)来检测运行代码的手势,而不是使用单根手指滑动来滚动 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.

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?

Thank you in advance.

Neal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街道布景 2024-10-18 06:20:53

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

使用 UIGestureRecognizerUISwipeGestureRecognizer

此外,如果您希望检测滑动方向,则必须设置两个单独的手势识别器。您无法从滑动手势识别器获取滑动方向,只能获取其注册识别的方向。

This goes in ApplicationDidLaunch:

UISwipeGestureRecognizer *swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreen:)] autorelease];
swipeGesture.numberOfTouchesRequired = 2;
    swipeGesture.direction = (UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown);

[window addGestureRecognizer:swipeGesture];

then implement

- (void) swipedScreen:(UISwipeGestureRecognizer*)swipeGesture {
   // do stuff
}

Use the documentation for UIGestureRecognizer and 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.

携君以终年 2024-10-18 06:20:53

在 swift 4.0 中,这发生在 AppDelegate 的 didFinishLaunchingWithOptions 方法上:

let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedScreen(swipeGesture:)))
swipeGesture.numberOfTouchesRequired = 2
swipeGesture.direction = [UISwipeGestureRecognizerDirection.up,  UISwipeGestureRecognizerDirection.down]
window?.addGestureRecognizer(swipeGesture)

以及操作:

@objc func swipedScreen(swipeGesture: UISwipeGestureRecognizer){
    Swift.print("hy")
}

In swift 4.0, that goes on the method didFinishLaunchingWithOptions of the AppDelegate:

let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedScreen(swipeGesture:)))
swipeGesture.numberOfTouchesRequired = 2
swipeGesture.direction = [UISwipeGestureRecognizerDirection.up,  UISwipeGestureRecognizerDirection.down]
window?.addGestureRecognizer(swipeGesture)

And the action:

@objc func swipedScreen(swipeGesture: UISwipeGestureRecognizer){
    Swift.print("hy")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文