简体   繁体   中英

UIWebview remove tap to focus

A UIWebView is normally focused on the selected content when I double tap a part of a website, and now I want the UIWebView to ignore the double tap but still be able to be interacted with.

How would I go about this?

The Double Tap is recognized by a UITapGestureRecognizer. Go through the view hierarchy if you really want this. A little bit tricky, but it should work.

The codes look like this:

- (void)goThroughSubViewFrom:(UIView *)view {
    for (UIView *v in [view subviews])
    {
        if (v != view)
        {
            [self goThroughSubViewFrom:v];
        }
    }
    for (UIGestureRecognizer *reco in [view gestureRecognizers])
    {
        if ([reco isKindOfClass:[UITapGestureRecognizer class]])
        {
            if ([(UITapGestureRecognizer *)reco numberOfTapsRequired] == 2)
            {
                [view removeGestureRecognizer:reco];
            }
        }
    }
}

I've put a demo here: NoDoubleTapWebView

Hope it helps.

iOS 5.0+:

A UIWebView contains a UIScrollView to display its content and is accessible by the scrollView property of UIWebView . So, one possible way of doing this is to subclass UIScrollView and override -touchesShouldBegin:withEvent:inContentView . You can get the number of taps from a UITouch object from its tapCount property, which can be used to filter out double-tap gestures and call the super method otherwise. This would look something like:

-(BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
    UITouch * touch = [touches anyObject];    //Get a touch object
    if (2 == touch.tapCount) {    //If this is a double-tap...
        return NO;                //...don't begin touches
    }
    else {    //Otherwise, call the superclass' implementation
        return YES;
    }
}

However, in order to set the web view's scroll view as your custom subclass, you may also have to subclass UIWebView (which is generally frowned upon). With that said, it would likely work to subclass UIWebView and redefine the scrollView property. In the interface file:

@interface MyAwesomeWebView : UIWebView {

}

@property (nonatomic, readonly, retain) MyAwesomeScrollView * scrollView;

@end

And in the implementation file:

@implementation

@dynamic scrollView

@end

Still, proceed with caution.

Pre iOS 5.0:

As Alan Moore wrote in the comments, you can add a transparent view on top of the web view, capture all touch events with hit testing, and forward the touch events to the web view unless there's a double tap.

EDIT: Updated answer to include caveats about subclassing UIWebView, etc. EDIT 2: Included second answer for pre iOS 5.0

I think the way to do it is to add a UITapGestureRecognizer on top of your UIWebView, then you can filter double taps as you wish.

You can find sample code/discussion here:

Add a UITapGestureRecognizer to a 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