将滑动手势放在UIWebView上以获取IOS中的滚动方向

时间:2015-06-16 04:13:09

标签: ios xcode scroll uiwebview swipe

我想在webView中获取滚动方向。我阅读了有关获取滚动方向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");
    }   
}

我有两个问题: -

  1. 关于此代码,我不知道在我的代码中调用userDidScrollWebView方法的位置,以便定期获得有关滚动的更新。

  2. 另一种方法,我想可能是我可以将Swipe Gesture放在Web View上,但这不起作用。

1 个答案:

答案 0 :(得分:1)

这是我在UIWebView中实现滑动手势的方式。

  1. ViewController.h协议添加到ViewDidLoad
  2. UISwipeGestureRecognizer * swipeGestureDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDown)]; swipeGestureDown.numberOfTouchesRequired = 1; swipeGestureDown.direction = UISwipeGestureRecognizerDirectionDown; swipeGestureDown.delegate = self; [self.webView addGestureRecognizer:swipeGestureDown];方法中添加以下代码:

    ViewController.m

  3. -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }中添加委托方法:

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

  4. <preference name="orientation" value="landscape"></preference>
  5. 同样,您可以添加另一个手势以向上滑动。