如何在ViewController中的UIWebView上处理SwipeGestures?

时间:2021-12-02 00:19:26

I am diving into iOS development and for that I am developing a simple app that displays blogposts.

我正在深入研究iOS开发,为此我正在开发一个显示博客帖子的简单应用程序。

I have the following setup in my DetailViewController:

我的DetailViewController中有以下设置:

如何在ViewController中的UIWebView上处理SwipeGestures?

What I want to implement is that when a user swipes left or right on the screen, I change the content of the webView.

我想要实现的是当用户在屏幕上向左或向右滑动时,我改变了webView的内容。

The swipe-gestures however seem to be swallowed by the UIWebView, so I figured I could make a transparent UIView ("Posts Detail Touch Interceptor View") on top and have it handle the swipe-gestures and forward them to the DetailViewController.

然而,滑动手势似乎被UIWebView吞噬了,所以我想我可以在顶部制作透明的UIView(“帖子细节触摸拦截器视图”)并让它处理滑动手势并将它们转发到DetailViewController。

This works, the swipe-handlers in DetailViewController get called, but I can no longer scroll in the webView or press any buttons. I assume, because the transparent UIView consumes all events before they reach the UIWebView.

这样可以调用DetailViewController中的滑动处理程序,但我无法再在webView中滚动或按任何按钮。我假设,因为透明的UIView在到达UIWebView之前会消耗所有事件。

What is the best way to make all controls below the transparent view respond to events normally, while the swipe gestures get forwarded to the DetailViewController?

使透明视图下方的所有控件正常响应事件的最佳方法是什么,而滑动手势会转发到DetailViewController?

1 个解决方案

#1


1  

You could still add the swipe recognizer to the UIWebView and implement this method in your gesture recognizer delegate:

您仍然可以将滑动识别器添加到UIWebView并在手势识别器委托中实现此方法:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

Alternatively, you can see this SO post to handle the event in both views:

或者,您可以在两个视图中看到此SO帖子来处理事件:

Touch event handled by multiple views

触摸事件由多个视图处理

I recommend first trying to use your own gesture recognizer in the UIWebView. I have done this before successfully.

我建议首先尝试在UIWebView中使用自己的手势识别器。我成功之前做过这个。

#1


1  

You could still add the swipe recognizer to the UIWebView and implement this method in your gesture recognizer delegate:

您仍然可以将滑动识别器添加到UIWebView并在手势识别器委托中实现此方法:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

Alternatively, you can see this SO post to handle the event in both views:

或者,您可以在两个视图中看到此SO帖子来处理事件:

Touch event handled by multiple views

触摸事件由多个视图处理

I recommend first trying to use your own gesture recognizer in the UIWebView. I have done this before successfully.

我建议首先尝试在UIWebView中使用自己的手势识别器。我成功之前做过这个。