子控制器view的懒加载

1.方法一: (传参, 扩展性强)

  • 在scrollView被创建之后, 让程序一开始的时候默认显示第 0 个子控制器的 View

    // 默认显示第0个子控制器的view
    [self addChildVcViewIntoScrollView:0];
    
  • 在按钮监听调用的方法中调用添加子控制器的 View 的方法

    // 监听按钮点击事件
    - (void)clickTitleButton:(UIButton *)titleButton{
        _previousClickTitleButton.selected = NO;
        titleButton.selected = !titleButton.selected;
        _previousClickTitleButton = titleButton;
    
        // 在这里添加对应的控制 View 到 scrollView 中
        [self addChildViewToScrollView:titleButton.tag];
    
        // 修改偏移量
        CGPoint Offset = _scrollView.contentOffset;
        Offset.x = titleButton.tag * _scrollView.CDH_width;
        [UIView animateWithDuration:0.3 animations:^{
            _titleUnderline.CDH_width = titleButton.titleLabel.CDH_width + 10;
            _titleUnderline.CDH_centerX = titleButton.CDH_centerX;
            _scrollView.contentOffset = Offset;
        }];
    }
    
  • 添加index位置的子控制器view到scrollView中

    // 添加子控制器的 View 到 scrollView
    - (void)addChildViewToScrollView:(NSInteger)index{
        // 添加子控制器的 View 到 scrollView
        UIViewController *ChildVc = self.childViewControllers[index];
    
        // 如果添加过了就不在添加
        if (ChildVc.isViewLoaded) return;
    
        ChildVc.view.frame = CGRectMake(index * _scrollView.CDH_width , 0 , _scrollView.CDH_width, _scrollView.CDH_height);
        [_scrollView addSubview:ChildVc.view];
    }
    
  • 在添加子控制器 view 到 scrollView 中的时候避免重复添加处理

        if (childVc.isViewLoaded) return;
    //    if (childVc.view.superview) return; // 表明已经在父控件的 VIEW 上
    //    if (childVc.view.window) return; // 表明已经被添加过到 window
    //    if ([self.scrollView.subviews containsObject:childVc.view]) return;// 表明已经存在这个 View
    


2. 方法二: (不传参, 依赖性比较强)

  • // 默认显示第0个子控制器的view
    [self addChildVcViewIntoScrollView];
    
    [UIView animateWithDuration:0.25 animations:^{
        // 下划线
        self.titleUnderline.CDH_width = titleButton.titleLabel.CDH_width + 10;
        self.titleUnderline.CDH_centerX = titleButton.CDH_centerX;
        // 添加子控制器view到scrollView中
        [self addChildVcViewIntoScrollView];
        // 滑动scrollView到对应的子控制器界面
        CGPoint offset = self.scrollView.contentOffset;
        offset.x = index * self.scrollView.CDH_width;
        self.scrollView.contentOffset = offset;
    } completion:^(BOOL finished) {}];
    
    /** 添加子控制器view到scrollView中 */
    - (void)addChildVcViewIntoScrollView{
        // 再次计算得到索引
        NSInteger index = self.scrollView.contentOffset.x / self.scrollView.CDH_width;
        UIView *childVcView = self.childViewControllers[index].view;
    
        childVcView.frame = self.scrollView.bounds;
    
    //    childVcView.frame = CGRectMake(self.scrollView.bounds.origin.x,
    //                                   self.scrollView.bounds.origin.y,
    //                                   self.scrollView.bounds.size.width,
    //                                   self.scrollView.bounds.size.height);
    
    //    childVcView.frame = CGRectMake(self.scrollView.contentOffset.x,
    //                                   self.scrollView.contentOffset.y,
    //                                   self.scrollView.CDH_width,
    //                                   self.scrollView.CDH_height);
    
    //    childVcView.frame = CGRectMake(index * self.scrollView.CDH_width,
    //                                   0,
    //                                   self.scrollView.CDH_width,
    //                                   self.scrollView.CDH_height);
    
        [self.scrollView addSubview:childVcView];
    }
    


results matching ""

    No results matching ""