Question

I have 2UIWebView controls. Using these 2 webviews, I have successfully implemented swipe gesture animation.

But the problem is, when I click on next or previous button(oh yes, I also have next, previous, first and last buttons to read a book), swipe works perfectly.

But on webview it works weirdly. Following happens:

  • Swipe doesn't work on webview.
  • When I click on next or previous buttons and then swipe the webview, swipe on webview works.

Following is my code snippet:

In viewDidLoad:

  UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionLeft];
[webViewPage addGestureRecognizer:swipeRight];
[_webview2 addGestureRecognizer:swipeRight];


UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionRight];
[webViewPage addGestureRecognizer:swipeLeft];
[_webview2 addGestureRecognizer:swipeLeft];

To enable swipe on UIWebView:

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
//    NSLog(@"shouldRecognizeSimultaneouslyWithGestureRecognizer");
    return YES;
}



 - (IBAction)btnPrevious_click:(id)sender {
   //some code

}

- (IBAction)btnNext_click:(id)sender {
    //some code
}

Where am I getting wrong?

Was it helpful?

Solution

From your code, you have to added 2 UISwipeGestureRecognizer in only one UIWebView name is _webview2 please change it as per your requirement.

OTHER TIPS

Like iPatel mentioned. Each of the webview should has its own GestureRecognizers. So, you should have 4 GestureRecognizers in total.

Try this:-

UISwipeGestureRecognizer *swipeRight1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
[swipeRight1 setDirection:UISwipeGestureRecognizerDirectionLeft];
UISwipeGestureRecognizer *swipeLeft1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];
[swipeLeft1 setDirection:UISwipeGestureRecognizerDirectionRight];
[_webview1 addGestureRecognizer:swipeRight1];
[_webview1 addGestureRecognizer:swipeLeft1];   

UISwipeGestureRecognizer *swipeRight2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
[swipeRight2 setDirection:UISwipeGestureRecognizerDirectionLeft];
UISwipeGestureRecognizer *swipeLeft2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];
[swipeLeft2 setDirection:UISwipeGestureRecognizerDirectionRight];
[_webview2 addGestureRecognizer:swipeRight2];
[_webview2 addGestureRecognizer:swipeLeft2];   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top