The WebView slides with the ScrollView

Since the project involves the sliding interaction between webView and scrollView, the first step is to calculate the height of webView and disable the sliding of webView, and then change the contentSize of scrollView to realize the sliding operation. But this approach when the webView loaded content with too many images will cause memory overflow, so a different approach to achieve.

# look like this:

Download the Demo

The code to handle

1. Processing of scrollView

(1) First you need to create an LQScrollView class that responds to multiple gestures

Methods for responding to multiple gestures

/** make ScrollView respond to multiple gestures */ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {return YES;
}
Copy the code
(2) Create a scrollView inherited from LQScrollView in controller, and create an isCanScroll BOOL property to control whether scrollView can slide
/** whether scrollView can slide */ @property (nonatomic, assign) BOOL isCanScroll; /** scrollV */ @property (nonatomic, strong) LQScrollView *scrollV;Copy the code

2. Processing of webView

(1) first need to create a LQWebView class, because the proxy method of ScrollView in webView is also – (void)scrollViewDidScroll:(UIScrollView *) ScrollView, so in order to better distinguish the proxy method implementation, An LQWebView should be created here to control the sliding proxy method
(2) Create a BOOL property (isWebCanScroll) to control whether the webView can slide
/ / @property (nonatomic, assign) BOOL isWebCanScroll;Copy the code
(3) Handle the sliding proxy of ScrollView in webView

The isWebCanScroll property is used to control whether the webView can slide or not, and the contentOffset of ScrollViews in webView is generally set to make the webView unslide. When the webView can slide and slide to the top, At this point, change the isWebCanScroll property of the webView to make the webView unslippery again, and send this status with a notification.

#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.scrollView) {
        CGFloat offY = scrollView.contentOffset.y;
//        NSLog(@"offY == %lf",offY);
        
        if(! Self. IsWebCanScroll) {/ / by setting the contentOffset let webView cannot slide the self. The scrollView. ContentOffset = CGPointZero; }ifSelf. isWebCanScroll = NO; self.isWebCanScroll = NO; self.isWebCanScroll = NO; self.isWebCanScroll = NO; self.scrollView.contentOffset = CGPointZero; [[NSNotificationCenter defaultCenter] postNotificationName:@"WEBVIEWSCROLLTOTOP"object:nil]; }}}Copy the code

3. Processing in Controller

(1) The agent that handles ScrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offY = scrollView.contentOffset.y;
    
    if (scrollView == self.scrollV) {
        if(offY > = TopViewHeight) {/ / at this point the webView reach the top let scrollView is slippery Let the webView can be slippery self. ScrollV. ContentOffset = CGPointMake (0, TopViewHeight);if(self.isCanScroll) { self.isCanScroll = NO; self.webV.isWebCanScroll = YES; }}else if(offY >= 0 &&offy < 200){//scrollView is in the slideable rangeif (self.isCanScroll) {
                self.scrollV.contentOffset = CGPointMake(0, offY);
            }else{
               if(self. WebV. IsWebCanScroll && self. The webV. The scrollView. ContentOffset. Y = = 0) {/ / solve the problem of critical value self. IsCanScroll = YES; self.webV.isWebCanScroll = NO; }else {
                    self.scrollV.contentOffset = CGPointMake(0, TopViewHeight);
                }
                
            }
        }
    }
    
    
}

Copy the code
(2) Handling notification events
*/ - (void)webViewScrollToTop:(NSNotification *)nofi{self.isCanScroll = YES; }Copy the code

Supplementary notes:

This works not only with webView and scrollView but also with tableView and scrollView. It is handled the same way, using the BOOL property to control slippability, which is handled through the ScrollView proxy method scrollViewDidScroll to control the contentOffset of ScrollView.

GitHub address: github.com/qiuyubude/G…

Jane address book: www.jianshu.com/p/5ee0c074a…