Search code examples
iosxcodescrolluiwebviewswipe

Put Swipe Gesture over UIWebView to Get Scroll Direction in IOS


I want to get the scroll direction in webView. I read the code about getting scroll direction here.

-(void)userDidScrollWebView:(id)scrollPoint{
    // NSLog(@"scrolled:::");

    NSString *x1 = [webView stringByEvaluatingJavaScriptFromString: @"scrollX"];


    NSString *y1 = [webView stringByEvaluatingJavaScriptFromString: @"scrollY"];


    NSLog(@"scroll x=%@ y=%@", x1,y1);      

    if ([y1 isEqualToString: @"0"]) {
        NSLog(@"RELAOD ME");
    }   
}

I have 2 Questions :-

  1. About this code,I don't know where to call userDidScrollWebView method in my code so that I get regular updates about scrolling.

  2. Another approach, I thought may be I could place Swipe Gesture over Web View but that's not working.


Solution

  • This is how I implemented swipe gesture in UIWebView.

    1. Add <UIGestureRecognizerDelegate> protocol to ViewController.h
    2. In ViewDidLoad Method add the following code:

      UISwipeGestureRecognizer * swipeGestureDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDown)]; swipeGestureDown.numberOfTouchesRequired = 1; swipeGestureDown.direction = UISwipeGestureRecognizerDirectionDown; swipeGestureDown.delegate = self; [self.webView addGestureRecognizer:swipeGestureDown];

    3. Add Delegate method in ViewController.m :

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

    4. -(void)swipeDown { NSLog(@"swipe down in webView"); }

    Similarly you can add another gesture for swipe Up.