【问题标题】:Get touchesBegan on a subview (UIWebView)在子视图 (UIWebView) 上获取 touchesBegan
【发布时间】:2012-03-20 09:47:44
【问题描述】:

我有一个 UIWebView,它作为子视图添加到 UIview 中,我想检测何时触摸此 UIWebView 但 touchesBegan 不起作用。

有什么想法吗?

【问题讨论】:

  • 显示相关代码如何?

标签: iphone objective-c ios uiwebview


【解决方案1】:
  1. 在您的代码中子类化 UIWebView,然后在您的类中实现 touchesBegan。
  2. 将子类 webview 添加为 UIView 的子视图。
  3. 在子类 UIWebView 中调用 super

    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    {
        [super touchesBegan: touches withEvent: event];
    }
    

【讨论】:

    【解决方案2】:

    子类化和等待 touchesBegan 对您没有帮助,因为它不会被调用。
    我对UIWebView 进行了子类化,并且只是在 2 级深的子视图中“吸食”了它的手势识别器(你可以递归地进行,但这对于 iOS6-7 来说已经足够了)。 然后你可以对触摸位置和手势识别器的状态做任何你想做的事情。

    for (UIView* view in self.subviews) {
        for (UIGestureRecognizer* recognizer in view.gestureRecognizers) {
            [recognizer addTarget:self action:@selector(touchEvent:)];
        }
        for (UIView* sview in view.subviews) {
            for (UIGestureRecognizer* recognizer in sview.gestureRecognizers) {
                [recognizer addTarget:self action:@selector(touchEvent:)];
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      斯威夫特 5

      subview.swift

      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
          super.touchesBegan(touches, with: event)
          self.next?.touchesBegan(touches, with: event)
      }
      

      如果您不确定是否应该致电super.touchesBegan(touches, with: event),请参阅文档discussion

      superview.swift

      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
          // handle
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多