Wooly
Wooly

Reputation: 343

iOS: Can I intercept swipes on top of a UIWebView?

I want the user to be able to swipe left, right, upward and downward on a UIWebview, but I don't want the HTML/Javascript to process those swipes-- I want to do that myself.

Is that possible: To intercept just the swipes? I want the taps still to go through to the UIWebview.

Thanks.

Upvotes: 1

Views: 2078

Answers (2)

tlbrack
tlbrack

Reputation: 926

If you add a UISwipeGestureRecognizer onto the UIWebview with

addGestureRecognizer: 

it will allow you to get those events. I believe the default is to only allow one recognizer at a time, but I'm not sure if that applies to individual gesture types or across all gesture types, so this may only be the first step in solving your problem.

Upvotes: 0

Ariel
Ariel

Reputation: 2440

Sure thing.
Just attach UIGestureRecognizer subclass to that view and hold on for calls...

    UISwipeGestureRecognizer* leftSwipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(someAction)];
    leftSwipeRecognizer.numberOfTouchesRequired = 1;
    leftSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    leftSwipeRecognizer.cancelsTouchesInView = YES;
    [self.webView addGestureRecognizer:leftSwipeRecognizer];

there is some sample for left swipe gesture. You can set more with very similar approach...
Hope that helps.

Upvotes: 5

Related Questions