iOS滑动View复用

@interface ViewController ()<UIScrollViewDelegate>

///所有试题数组
@property (nonatomic,strong) NSArray *arrayQuestin;

///UIScrollView
@property (nonatomic,strong) UIScrollView *scrollview;

///保存可见的视图
@property (nonatomic, strong) NSMutableSet *visibleViewControllers;

/// 保存可重用的
@property (nonatomic, strong) NSMutableSet *reusedViewControllers;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _arrayQuestin = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",];
    [self.view addSubview:self.scrollview];
    
    [self showVc];
    
}

- (NSMutableSet *)visibleViewControllers {
    if (!_visibleViewControllers) {
        _visibleViewControllers = [[NSMutableSet alloc]init];
    }
    return _visibleViewControllers;
}

- (NSMutableSet *)reusedViewControllers {
    if (!_reusedViewControllers) {
        _reusedViewControllers = [[NSMutableSet alloc]init];
    }
    return _reusedViewControllers;
}

- (UIScrollView *)scrollview {
    if (!_scrollview) {
        _scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_Width, SCREEN_Height)];
        _scrollview.pagingEnabled = YES;
        _scrollview.backgroundColor = [UIColor whiteColor];
        _scrollview.delegate = self;
        _scrollview.contentSize = CGSizeMake(SCREEN_Width * self.arrayQuestin.count, SCREEN_Height);
    }
    return _scrollview;
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    ///更新模板信息
    [self showVc];
}

///显示试图
- (void)showVc{
    // 获取当前处于显示范围的 控制器 索引
    CGRect visibleBounds = self.scrollview.bounds;
    CGFloat minX  = CGRectGetMinX(visibleBounds);
    CGFloat maxX  = CGRectGetMaxX(visibleBounds);
    CGFloat width = CGRectGetWidth(visibleBounds);
    NSInteger firstIndex = (NSInteger)floorf(minX / width);
    NSInteger lastIndex  = (NSInteger)floorf(maxX / width);
    
    // 处理越界
    if (firstIndex < 0) {
        firstIndex = 0;
    }
    if (lastIndex >= self.arrayQuestin.count) {
        lastIndex = (self.arrayQuestin.count - 1);
    }
    // 回收掉不在显示的
    NSInteger viewIndex = 0;
    for (QuestViewController * vc in self.visibleViewControllers) {
        viewIndex = vc.index;
        // 不在显示范围内
        if ( viewIndex < firstIndex || viewIndex > lastIndex) {
            [self.reusedViewControllers addObject:vc];
            [vc removeFromParentViewController];
            [vc.view removeFromSuperview];
        }
    }
    [self.visibleViewControllers minusSet:self.reusedViewControllers];
    // 是否需要显示新的视图
    for (NSInteger index = firstIndex; index <= lastIndex; index ++) {
        BOOL isShow = NO;
        for (QuestViewController * childVc in self.visibleViewControllers) {
            
            if (childVc.index == index) {
                isShow = YES;
            }
        }
        if (!isShow ) {
            [self showVcWithIndex:index];
        }
    }
}

// 显示一个 view
- (void)showVcWithIndex:(NSInteger)index{
    QuestViewController *vc = [self.reusedViewControllers anyObject];
    if (vc) {
        [self.reusedViewControllers removeObject:vc];
        
    }else{
        QuestViewController *childVc = [[QuestViewController alloc] init];
        [self addChildViewController:childVc];
        vc = childVc;
    }
    CGRect bounds  = self.scrollview.bounds;//654
    CGRect vcFrame = bounds;
    vcFrame.origin.x = CGRectGetWidth(bounds) * index;
//    vc.rectView = vcFrame;
    vc.index = index;
    vc.view.frame = vcFrame;
    [self.scrollview addSubview:vc.view];
    [self.visibleViewControllers addObject:vc];
    
    // 最后在这个地方,更新模板VC中的信息
    ///更新信息处理
    
    vc.label.text = self.arrayQuestin[index];
}

猜你喜欢

转载自blog.csdn.net/weixin_34248023/article/details/87026308