//
// ViewController.m
// ScollerView模拟tableView重用机制
//
// Created by lixiang on 19/2/16.
// Copyright (c) 2016 lixiang. All rights reserved.
//
#import “ViewController.h”
#import “LXModel.h”
#import “LXButton.h”
@interface ViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *arr;// 托管重用数组
@property (nonatomic, assign) CGFloat upY;// 需要添加到头部子视图的坐标Y值
@property (nonatomic, assign) CGFloat downY;// 需要添加到尾部子视图的坐标Y
@property (nonatomic, assign) CGFloat offset;// 拖动时候的偏移量
@property (nonatomic, strong) NSMutableArray *allArray;
@property (nonatomic, assign) NSInteger rankRecord;//记录当前最后一个box是10个box中的第几个。默认值为5,滚动时+1或-1,下限为5,上限是self.allArray.count
@property (nonatomic, assign) int screenWidth;
@property (nonatomic, assign) int screenHeight;
@property (nonatomic, assign) int boxHeight;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
self.screenWidth = self.view.frame.size.width;
self.screenHeight = self.view.frame.size.height;
self.boxHeight = self.view.frame.size.width*0.625;
[self requestData];//请求全部的数据
}
// 请求数据
– (void)requestData {
for (int i = 0; i < 10; i++) {
LXModel *model = [[LXModel alloc]init];
model.title = [NSString stringWithFormat:@”%d”,i];
[self.allArray addObject:model];
}
// 若请求数据成功 就初始化一个ScrollView
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.screenWidth, self.screenHeight)];
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 10000);
self.scrollView.backgroundColor = [UIColor lightGrayColor];
self.scrollView.delegate = self;
[self.view addSubview:self.scrollView];
// 若请求的数据中的数组数量大于5 ,就启用重用
if (self.allArray.count > 5) {
self.rankRecord = 5;
NSInteger i = 0;
for (i = 0; i < 5; i++) {
LXButton *box = [[LXButton alloc] initWithFrame:CGRectMake(0, i * self.boxHeight , self.screenWidth, self.boxHeight)];
box.rank = i;
LXModel *model = self.allArray[i];
box.model = model;
[self.scrollView addSubview:box];// 添加子视图到scrollview
[self.arr addObject:box];// 添加view到托管的重用数组
}
// 下一个view的y点是
i++;
self.downY = i * self.boxHeight;
self.upY = –self.boxHeight;
self.offset = 0;
}
}
– (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
self.offset = scrollView.contentOffset.y; // 记录开始拖动时的偏移量
}
– (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// 判断上下滑 是否偏移量超出屏幕
if (scrollView.contentOffset.y < 0 || scrollView.contentOffset.y > (10000 – self.screenHeight)) {
// 超出屏幕范围不做任何操作
} else if ((scrollView.contentOffset.y – self.offset) > 0) {
// 上拉屏幕
if ((self.downY – (self.scrollView.contentOffset.y + self.screenHeight)) < self.boxHeight+50 && self.rankRecord < self.allArray.count) {//√
LXButton *box = [self.arr objectAtIndex:0];// 取出数组的第一个元素
self.upY = box.frame.origin.y;// 将头部Y坐标变为box的Y坐标 因为box要添加到尾部
[self.arr removeObjectAtIndex:0];// 移除数组第一个元素
box.frame = CGRectMake(0, self.downY – self.boxHeight, self.screenWidth, self.boxHeight);// 改变box的frame
box.rank = box.rank+5; // 改变frame的同时改变索引
LXModel *model = self.allArray[box.rank]; // 根据索引取出模型
box.model = model; // 赋值
[self.arr addObject:box];// 将box重新添加到数组
self.rankRecord ++;//记录当前最后一个box是10个box中的第几个
self.downY = self.downY + self.boxHeight;// 改变下一个的尾部坐标的Y值//////////////////////////////
NSLog(@”上拉%f”,self.downY);
}
} else if((scrollView.contentOffset.y – self.offset) < 0){
// 下拉屏幕
if ((self.scrollView.contentOffset.y – self.upY) < self.boxHeight+50 && self.rankRecord>5) {//!!!
LXButton *box = [self.arr lastObject];// 取出数组的最后一个元素
self.downY = box.frame.origin.y+self.boxHeight;// 将尾部Y坐标变为box的Y坐标 因为box要添加到头部部///////////////////////////////////
[self.arr removeObject:box];// 移除数组最后一个元素
box.frame = CGRectMake(0, self.upY, self.screenWidth, self.boxHeight);// 改变box的frame
box.rank = box.rank – 5; // 改变frame的同时改变索引
LXModel *model = self.allArray[box.rank]; // 根据索引取出模型
box.model = model; // 赋值
[self.arr insertObject:box atIndex:0];// 将box重新添加到数组的头部
self.upY = self.upY – self.boxHeight;// 改变下一个的头部坐标的Y值
self.rankRecord –;//记录当前最后一个box是10个box中的第几个
NSLog(@”下拉%f”,self.downY);
}
}
}
#pragma mark 懒加载
– (NSMutableArray *)arr {
if (_arr == nil) {
_arr = [[NSMutableArray alloc] init];
}
return _arr;
}
– (NSMutableArray *)allArray {
if (_allArray == nil) {
self.allArray = [NSMutableArray array];
}
return _allArray;
}
@end