ios ~ UIScrollView滑动方向、其上两个WKWebView

1、
// 在这里,因为self.homeScrollView.frame没有渲染,没有值,所以需要要在网络请求成功时,重新赋值其frame
    
    /**
     // 【设置水平滚动,禁止垂直滚动】
     if (!self.homeScrollView.contentSize.width) {
         self.homeScrollView.contentSize = CGSizeMake(self.homeScrollView.frame.size.width*2, 0);
     }
     */

// 【设置水平滚动,禁止垂直滚动】
                if (!self.homeScrollView.contentSize.width) {
                    self.homeScrollView.contentSize = CGSizeMake(self.homeScrollView.frame.size.width*2, 0);
                }
                
                self.newsTableView.frame = CGRectMake(0, 0, self.homeScrollView.frame.size.width, self.homeScrollView.frame.size.height);
                self.bookTableView.frame = CGRectMake(self.homeScrollView.frame.size.width, 0, self.homeScrollView.frame.size.width, self.homeScrollView.frame.size.height);

2、例子

// 导入头文件
#import 
//遵守代理:

- (void)setupWebView {
    
    UIEdgeInsets insets = UIEdgeInsetsMake(20, 0, 0, 0);
    if (@available(iOS 11.0, *)) {
        insets = [UIApplication sharedApplication].keyWindow.safeAreaInsets;
    }
    
    _myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, [UIScreen mainScreen].bounds.size.height - insets.top - 44)];
    _myScrollView.backgroundColor = [UIColor greenColor];
    self.myScrollView.delegate = self;
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.showsHorizontalScrollIndicator = NO;
    self.myScrollView.scrollEnabled = YES;
    [self.view addSubview:self.myScrollView];

    // 手势互斥(侧滑返回手势失效后才响应UITableView的滑动手势)
    [self.myScrollView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
    
    /// 原文 webView
    _originalWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.myScrollView.frame.size.width, self.myScrollView.frame.size.height)];
//    self.originalWebView.backgroundColor = [UIColor yellowColor];
    self.originalWebView.navigationDelegate = self;
    [self.myScrollView addSubview:self.originalWebView];
    
    // type 1 News, 2 精讲
    // 转一下码,以适应网络链接:NSCharacterSet
    NSString *originUrlStr = [[NSString stringWithFormat:@"token=%@&user_uuid=%@&news_uuid=%@&type=1", tokenStr, wy_user_uuid, self.news_uuid] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    originUrlStr = [NSString 
    originUrlStr = [AFShareManager UrlString:[NSString stringWithFormat:@"/index.html#/originalText?%@", originUrlStr]];
//    NSURL *originalWebUrl = [NSURL URLWithString:originUrlStr];
//    NSLog(@"request =======>>>>>>>>  %@", originalWebUrl);
    NSMutableURLRequest *requestUrl = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:originUrlStr]];
    
    [self.originalWebView loadRequest:requestUrl];
    
    
    /// 详解 webView
    _explainWebView = [[WKWebView alloc] initWithFrame:CGRectMake(self.myScrollView.frame.size.width, 0, self.myScrollView.frame.size.width, self.myScrollView.frame.size.height)];
//    self.explainWebView.backgroundColor = [UIColor redColor];
    self.explainWebView.navigationDelegate = self;
    [self.myScrollView addSubview:self.explainWebView];
    NSString *explainUrlStr = [[NSString stringWithFormat:@"token=%@&user_uuid=%@&news_uuid=%@&type=1", tokenStr, wy_user_uuid, self.news_uuid] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];

   
    explainUrlStr = [AFShareManager UrlString:[NSString stringWithFormat:@"/index.html#/modules?%@", explainUrlStr]];
//    NSURL *explainUrl = [NSURL URLWithString:explainUrlStr];
//    NSLog(@"request =======>>>>>>>>  %@", explainUrl);
    NSMutableURLRequest *requestExplainUrl = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:explainUrlStr]];
    [self.explainWebView loadRequest:requestExplainUrl];
    
    // 【设置水平滚动,禁止垂直滚动】
    if (!self.myScrollView.contentSize.width) {
        self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width*2, 0);
    }
    self.originalWebView.frame = CGRectMake(0, 0, self.myScrollView.frame.size.width, self.myScrollView.frame.size.height);
    self.explainWebView.frame = CGRectMake(self.myScrollView.frame.size.width, 0, self.myScrollView.frame.size.width, self.myScrollView.frame.size.height);
}


- (void)clickOriginalTextAction {
    // 原文
    self.leftLine.hidden = NO;
    self.rightLine.hidden = YES;
    
    self.myScrollView.contentOffset = CGPointMake(0, 0);
    
    [self.leftBut setTitleColor:RGBA(0, 0, 0, 1) forState:UIControlStateNormal];
    [self.rightBut setTitleColor:RGBA(153, 153, 153, 1) forState:UIControlStateNormal];
}

- (void)clickExplainAction {
    // 详解
    self.leftLine.hidden = YES;
    self.rightLine.hidden = NO;
    
    self.myScrollView.contentOffset = CGPointMake(self.myScrollView.frame.size.width, 0);
    
    [self.leftBut setTitleColor:RGBA(153, 153, 153, 1) forState:UIControlStateNormal];
    [self.rightBut setTitleColor:RGBA(0, 0, 0, 1) forState:UIControlStateNormal];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.myScrollView) {
        if (scrollView.contentOffset.x < self.myScrollView.frame.size.width) {
            
            self.leftLine.hidden = NO;
            self.rightLine.hidden = YES;
            [self.leftBut setTitleColor:RGBA(0, 0, 0, 1) forState:UIControlStateNormal];
            [self.rightBut setTitleColor:RGBA(153, 153, 153, 1) forState:UIControlStateNormal];
            
        } else if (scrollView.contentOffset.x >= self.myScrollView.frame.size.width) {
            
            self.leftLine.hidden = YES;
            self.rightLine.hidden = NO;
            [self.leftBut setTitleColor:RGBA(153, 153, 153, 1) forState:UIControlStateNormal];
            [self.rightBut setTitleColor:RGBA(0, 0, 0, 1) forState:UIControlStateNormal];
            
        }
    }
}

你可能感兴趣的:(ios ~ UIScrollView滑动方向、其上两个WKWebView)