zoukankan      html  css  js  c++  java
  • 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];
  • 相关阅读:
    Java基础之线程最详细最精简的介绍
    Android基础之Android硬件
    Iphone客户端程序员半年工作总结
    Java基础之synchronized的讲解
    物联网能否落地?可裁剪嵌入式OS成关键
    java基础之Thread与Runnable的区别
    Android基础之小问题集锦
    Java基础之字符串匹配大全
    BPM 是与非 什么是BPM,如何辨别是否BPM产品,以及如何选择BPM产品
    Hello China操作系统的安装和使用
  • 原文地址:https://www.cnblogs.com/StevenHuSir/p/10114967.html
Copyright © 2011-2022 走看看