简体   繁体   中英

How to detect touch on UIWebView

On UIWebview, how can I detect a touch?

But not when user clicks some URL or touching a control.

Is it possible to handle it?

Use UIGestureRecognizerDelegate method:

Add UIGestureRecognizerDelegate in declaration file (ie your .h file)

Step 1: Just set the delegate of gestureRecognizer: (in .m file viewDidLoad)

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

Step 2: Override this function: (in .m file)

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

Step 3: Now implement the tapAction function:

- (void)tapAction:(UITapGestureRecognizer *)sender
{    
    NSLog(@"touched");

    // Get the specific point that was touched
    CGPoint point = [sender locationInView:self.view];
}

The accepted answer is great if you only need to detect taps. If you need to detect all touches, the best way is to create a new UIView subclass and place it over the webview. In the subclass you can detect touches using 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

Note that the accepted answer above will only capture tap gestures (touchDown and touchUp without a drag in between), and that swipe gestures will be ignored.

For my purposes I needed to be informed of both, and so I added swipe gesture recognizers appropriately. (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).

// 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];
}

Everything that inherits from UIResponder can handle touches (so does UIWebView). Read the doc: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html

You'll have to use:

touchesBegan:withEvent:

Edit: Adding the comment here for clarity-

I believe then there's no clean way of doing it, you can either override the hittest withEvent method like this or do a hack like this: overriding UIView

Do you mean you want to override the options that popup when they hold down on a link? I managed to get one to work with this tutorial/guide but the one posted here is still slightly buggy and needs you to do some fine tuning: http://www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM