【问题标题】:How to detect touch on UIWebView如何检测 UIWebView 上的触摸
【发布时间】:2011-06-11 16:54:46
【问题描述】:

在 UIWebview 上,如何检测触摸?

但不是当用户单击某个 URL 或触摸控件时。

可以处理吗?

【问题讨论】:

    标签: iphone uiwebview touch


    【解决方案1】:

    从 UIResponder 继承的所有东西都可以处理触摸(UIWebView 也是如此)。阅读文档: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html

    你必须使用:

    touchesBegan:withEvent:
    

    编辑:为了清楚起见,在此处添加评论-

    我相信没有干净的方法可以做到这一点,您可以像 this 这样覆盖 hittest withEvent 方法,或者像这样进行破解:overriding UIView

    【讨论】:

    • 我知道。但是当我对 uiwebview 进行子类化并实现 touchesBegan 和 touchesEnded 时,它们根本没有被调用.....
    • 在这种情况下,您可以在 webview 上创建一个透明的 UIView 并在其上使用 touches 方法。
    • 如果我将透明的 UIView 放在上面,它将失去 webview 的默认功能来点击链接或触摸控件等.....我说的对吗?
    • 我相信没有干净的方法可以做到这一点,您可以像 iphonedevsdk.com/forum/iphone-sdk-development/… 这样覆盖 hittest withEvent 方法,或者像这样 github.com/psychs/iphone-samples/blob/… 进行黑客攻击
    • 谢谢,我一定会看看的。你来自印度班加罗尔吗?可以给我你的联系方式吗.....讨论更多
    【解决方案2】:

    您的意思是要覆盖按住链接时弹出的选项吗?我设法让一个与本教程/指南一起工作,但这里发布的那个仍然有点错误,需要你做一些微调: http://www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/

    【讨论】:

    • 没有。我在网络视图上有一个关闭按钮。当用户触摸网络视图时,我想隐藏它。再次触摸时,它将再次显示关闭按钮。但我无法检测到网络视图上的触摸。
    • 您可以修改该教程以检测您想要监控的触摸类型,而不是在示例中查找的点击并按住操作。
    【解决方案3】:

    使用 UIGestureRecognizerDelegate 方法:

    在声明文件(即您的 .h 文件)中添加 UIGestureRecognizerDelegate

    第1步:只需设置gestureRecognizer的委托:(在.m文件viewDidLoad中)

    UITapGestureRecognizer *webViewTapped = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    webViewTapped.numberOfTapsRequired = 1;
    webViewTapped.delegate = self;
    [offScreenWebView addGestureRecognizer:webViewTapped];
    [webViewTapped release];
    

    第 2 步:覆盖此函数:(在 .m 文件中)

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

    第 3 步:现在实现 tapAction 函数:

    - (void)tapAction:(UITapGestureRecognizer *)sender
    {    
        NSLog(@"touched");
    
        // Get the specific point that was touched
        CGPoint point = [sender locationInView:self.view];
    }
    

    【讨论】:

      【解决方案4】:

      请注意,上面接受的答案只会捕获 tap 手势(touchDown 和 touchUp 之间没有拖动),并且滑动手势将被忽略。

      出于我的目的,我需要了解这两者,因此我适当地添加了滑动手势识别器。 (请注意,尽管是位字段,但您不能 OR 一起滑动手势识别器的 direction 属性,因此需要 4 个手势识别器来检测任何滑动)。

      // Note that despite being a bit field, you can't `OR` together swipe gesture
      // recognizers' `direction` property, so 4 gesture recognizers are required
      // to detect any swipe
      for (NSNumber * swipeDirection in @[@(UISwipeGestureRecognizerDirectionUp), @(UISwipeGestureRecognizerDirectionDown), @(UISwipeGestureRecognizerDirectionLeft), @(UISwipeGestureRecognizerDirectionRight)]) {
      
          UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(timerReset:)];
          swipe.direction = [swipeDirection integerValue];
          swipe.delegate = self;
          [rootWebView addGestureRecognizer:swipe];
      }
      

      【讨论】:

        【解决方案5】:

        如果您只需要检测水龙头,那么接受的答案就很好。如果需要检测所有的触摸,最好的方法是创建一个新的 UIView 子类并将其放在 webview 上。在子类中,您可以使用 hitTest 检测触摸:

        TouchOverlay.h

        @class TouchOverlay;
        
        @protocol TouchOverlayDelegate <NSObject>
        
        @optional
        - (void)touchOverlayTouched:(TV4TouchOverlay *)touchOverlay;
        
        @end
        
        
        @interface TouchOverlay : UIView
        @property (nonatomic, unsafe_unretained) id <TouchOverlayDelegate> delegate;
        @end
        

        Touchoverlay.m

        @implementation TouchOverlay
        
        - (id)initWithFrame:(CGRect)frame {
          self = [super initWithFrame:frame];
          return self;
        }
        
        - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
          UIView *hitView = [super hitTest:point withEvent:event];
          if (hitView == self) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(touchOverlayTouched:)]) {
              [self.delegate touchOverlayTouched:self];
            }
            return nil;     // Tell the OS to keep looking for a responder
          }
          return hitView;
        }
        
        @end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-06-17
          • 1970-01-01
          • 2013-10-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-30
          相关资源
          最近更新 更多