iOS WebView嵌套在ScrollView里

目的: 有时候需求是scrollView 嵌套scrollView,但是手势就存在冲突问题。文本简单处理冲突问题。

结构图:

iOS WebView嵌套在ScrollView里_第1张图片
结构图.png

直接代码:

// 处理 scrollview里面嵌套webview
- (void)scrollViewDidScroll:(UIScrollView *)scrollView  {
// 判断是否是scrollView里面的scrollView
    if (scrollView == _msgWebView.scrollView) {
        CGFloat y = scrollView.contentOffset.y;
        if (y > 0) {
            // 头部 向上走
            if (self.mailScrollView.contentOffset.y < self.topView.height) {
                self.mailScrollView.contentOffset = CGPointMake(0, self.mailScrollView.contentOffset.y + y);
                _msgWebView.scrollView.contentOffset = CGPointMake(0, 0);
                return;
            }
            
            // 尾部
            // _msgWebView 最底部时候的 Y
           CGFloat bottomY = (_msgWebView.scrollView.contentSize.height - _msgWebView.height);
            if (_msgWebView.scrollView.contentOffset.y > bottomY) {
                
                CGFloat addY = _msgWebView.scrollView.contentOffset.y - bottomY;
                
                if ((self.mailScrollView.contentOffset.y + addY) > (self.mailScrollView.contentSize.height - self.mailScrollView.height)) {
                    self.mailScrollView.contentOffset = CGPointMake(0, self.mailScrollView.contentSize.height - self.mailScrollView.height);
                }else{
                    self.mailScrollView.contentOffset = CGPointMake(0, self.mailScrollView.contentOffset.y+addY);
                }
                
                _msgWebView.scrollView.contentOffset = CGPointMake(0, bottomY);
            }else{
                
                
                if (_msgWebView.scrollView.contentOffset.y <= bottomY) {
                    
                    CGFloat mailScrollViewY = self.mailScrollView.contentOffset.y;
                    
                    CGFloat maxScrollViewY = self.mailScrollView.contentSize.height - self.mailScrollView.height;
                    CGFloat minScrollViewY = self.mailScrollView.contentSize.height - self.mailScrollView.height - self.attachmentView.height;
                    // 判断底部View是否显示出来了
                    if (mailScrollViewY > minScrollViewY && mailScrollViewY <= maxScrollViewY) {
                        // 移动mailScrollView
                        CGFloat bootomScrollY = bottomY - (_msgWebView.scrollView.contentOffset.y);
                        self.mailScrollView.contentOffset = CGPointMake(0, self.mailScrollView.contentOffset.y- bootomScrollY);
                        _msgWebView.scrollView.contentOffset = CGPointMake(0, bottomY);
                    }
                    // _msgWebView 内部滑动
                    return;
                }
            }
            
        }else{
            
            CGFloat mailContentOffY = self.mailScrollView.contentOffset.y + y;
            if (mailContentOffY < 0) {
                self.mailScrollView.contentOffset = CGPointMake(0, 0);
            }else{
                self.mailScrollView.contentOffset = CGPointMake(0, mailContentOffY);
            }
            _msgWebView.scrollView.contentOffset = CGPointMake(0, 0);
        }
    }
}

解决问题~

你可能感兴趣的:(iOS WebView嵌套在ScrollView里)