iOS-懒加载子控制器的View

①笨方法:

// 添加子控制器的view
    NSUInteger count = self.childViewControllers.count;
    CGFloat scrollViewW = scrollView.width;
    CGFloat scrollViewH = scrollView.height;
    for (NSUInteger i = 0; i < count; i++) {
        // 取出i位置子控制器的view
        UIView *childVcView = self.childViewControllers[i].view;
        //TableViewController默认Y = 20
        childVcView.frame = CGRectMake(i * scrollViewW, 0, scrollViewW, scrollViewH);
        [scrollView addSubview:childVcView];
    }
    //如果TableView和scrollView都能向上滑动,这个事件交由TableView处理
   scrollView.contentSize = CGSizeMake(count * scrollViewW, 0);//设置左右滑动

②懒加载(滑动完成之后再加载)

- (void)titleButtonClick:(HKTitleButton *)titleButton
{
    self.previousClickedTitleButton.selected = NO;
    titleButton.selected = YES;
    self.previousClickedTitleButton = titleButton;
    
    //优化加载方式,懒加载控制器
    NSUInteger index = titleButton.tag;
    [UIView animateWithDuration:0.25 animations:^{
        // 处理下划线
        self.titleUnderline.width = titleButton.titleLabel.width + 10;
        self.titleUnderline.centerX = titleButton.centerX;
        
        // 滚动scrollView
        CGFloat offsetX = self.scrollView.width * index;
        self.scrollView.contentOffset = CGPointMake(offsetX, self.scrollView.contentOffset.y);
    } completion:^(BOOL finished) {
        // 添加子控制器的view
        [self addChildVcViewIntoScrollView:index];
    }];
}
/**
 *  添加第index个子控制器的view到scrollView中
 */
- (void)addChildVcViewIntoScrollView:(NSUInteger)index
{
    //取出按钮索引对应的控制器
    UIViewController *childVc = self.childViewControllers[index];
    
    // 如果view已经被加载过,就直接返回
    if (childVc.isViewLoaded) return;
    
    // 取出index位置对应的子控制器view
    UIView *childVcView = childVc.view;
    //    if (childVcView.superview) return;
    //    if (childVcView.window) return;
    // 设置子控制器view的frame
    CGFloat scrollViewW = self.scrollView.width;
    childVcView.frame = CGRectMake(index * scrollViewW, 0, scrollViewW, self.scrollView.height);
    // 添加子控制器的view到scrollView中
    [self.scrollView addSubview:childVcView];
}


③懒加载-不使用Index(滑动完成之后再加载)

- (void)titleButtonClick:(HKTitleButton *)titleButton
{
    self.previousClickedTitleButton.selected = NO;
    titleButton.selected = YES;
    self.previousClickedTitleButton = titleButton;
    
    //优化加载方式,懒加载控制器
    NSUInteger index = titleButton.tag;
    [UIView animateWithDuration:0.25 animations:^{
        // 处理下划线
        self.titleUnderline.width = titleButton.titleLabel.width + 10;
        self.titleUnderline.centerX = titleButton.centerX;
        
        // 滚动scrollView
        CGFloat offsetX = self.scrollView.width * index;
        self.scrollView.contentOffset = CGPointMake(offsetX, self.scrollView.contentOffset.y);
    } completion:^(BOOL finished) {
        // 添加子控制器的view
        [self addChildVcViewIntoScrollView];
    }];
}
/**
 *  添加子控制器的view到scrollView中
 */
- (void)addChildVcViewIntoScrollView
{
    CGFloat scrollViewW = self.scrollView.width;
    NSUInteger index = self.scrollView.contentOffset.x / scrollViewW;
    // 取出index位置对应的子控制器view
    UIView *childVcView = self.childViewControllers[index].view;
    // 设置子控制器view的frame
    childVcView.frame = self.scrollView.bounds;
    // 添加子控制器的view到scrollView中
    [self.scrollView addSubview:childVcView];
    //HKLog(@"%zd--%@",index,childVcView);
    //    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.bounds.size.width, self.scrollView.bounds.size.height);
    //    childVcView.frame = CGRectMake(self.scrollView.contentOffset.x, self.scrollView.contentOffset.y, scrollViewW, self.scrollView.xmg_height);
    //    childVcView.frame = CGRectMake(self.scrollView.contentOffset.x, 0, scrollViewW, self.scrollView.xmg_height);
    //    childVcView.frame = CGRectMake(index * scrollViewW, 0, scrollViewW, self.scrollView.xmg_height);
}

ViewDidLoad默认加载第0个控制器的View

// 添加第0个子控制器的view
//[self addChildVcViewIntoScrollView:0];
[self addChildVcViewIntoScrollView];

 

posted @ 2018-12-13 16:50  淡然微笑_Steven  阅读(727)  评论(0编辑  收藏  举报