【问题标题】:iOS UITapGestureRecognizer and UIWebViewiOS UITapGestureRecognizer 和 UIWebView
【发布时间】:2023-03-17 14:20:02
【问题描述】:

我有一个视图控制器,其中包含一个网络视图。

我想在 viewController 的视图中添加两个手势识别器: 一个 UITapGestureRecognizer 和一个 UILongPressureGestureRecognizer。

代码如下:

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(backToDashboards:)];
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backToDashboards:)];

doubleTapGesture.numberOfTapsRequired = 2;
doubleTapGesture.delegate = self;

[self.view addGestureRecognizer:longPressGesture];
[self.view addGestureRecognizer:doubleTapGesture];

我的视图层次结构:

-ViewController
 |
 |-View (added the gesture recognizers for this view)
   |-WebView

我已经用return YES 添加了shouldRecognizeSimultaneouslyWithGestureRecognizer 方法

但是如果我长按屏幕什么都不会发生,只有当按下的位置位于 web 视图中无法滚动的地方时。

双击识别器根本不起作用。

有什么想法吗?

提前谢谢你!

【问题讨论】:

  • 我不知道对不起。
  • 我只是查看了我的手势识别器,没有一个选择器接受参数。尝试去掉 (backToDashboards:) 上的冒号,并在 backToDashBoards 方法中添加一条日志语句。
  • 参数是发送者
  • 感谢 kennyevo,我认为手势识别器没有发送者。现在我知道了。

标签: ios uiwebview uigesturerecognizer


【解决方案1】:

我怀疑因为你的 webview 在你的视图之上,所以应用程序不知道你是在点击 webview 还是 self.view。看看这个回复How to detect of tap gesture is from uiwebview or self.view?

【讨论】:

  • 嗨!感谢您的回答,但我认为这不是问题所在。我编辑了原始问题。 (添加了我的视图层次结构)
【解决方案2】:

试试下面的代码。你在哪里添加gester? 下面的代码对我有用。

 - (void)viewDidLoad
    { 
      [super viewDidLoad];
      UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
      longPress.numberOfTouchesRequired = 2;
      [self.view addGestureRecognizer:longPress];

      // if u want to add gesture for u webview,
      //[self.webview addGestureRecognizer:longPress]
    }

    -(void)handleLongPress:(UILongPressGestureRecognizer *)gesture
    {
    }

    //You should enable simultaneous gesture recognition   

          - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
            {
                return YES;
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    相关资源
    最近更新 更多