UIScrollView的重用笔记
UIScrollView 的重用原理借鉴TableView的重用池,创建一个专门盛放子View数组,下拉的时候 当拉到最底部的时候把scrollView顶部子视图移除并添加到底部,并且把数组index = 0 的元素放到最后面;上拉的时候 当拉到最顶部的时候把scrollView底部子视图移除并添加到顶部,并且把数组最后一个元素插入到最顶部。
注意:可优化部分为上拉到scrollView到最底部的时候 要把scrollView的contentSize扩大,以实现可无限上拉的效果
import "ViewController.h"
define logFunc NSLog(@"%s",__FUNCTION__) #define kCellHeight [UIScreen mainScreen].bounds.size.height/2.5 #define kCellWidth [UIScreen mainScreen].bounds.size.width-10 #define kScreenHeight [UIScreen mainScreen].bounds.size.height #define kScreenWidth [UIScreen mainScreen].bounds.size.width
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, assign)CGFloat upY;///顶部视图的y
@property (nonatomic, assign)CGFloat downY;尾部视图的y
@property (nonatomic, assign)CGFloat startOffSet;开始拖动的时候的偏移量
@property (nonatomic, strong)UIImageView *imageView;
@property (nonatomic, strong)NSMutableArray *viewArr;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self scrollView];
[self creatImageViewsOnScrollView];
}
- (NSMutableArray *)getImgsArray
{
NSMutableArray *arr = [[NSMutableArray alloc]init];
for (int i = 0; i < 7; i++) {
[arr addObject:[UIImage imageNamed:[NSString stringWithFormat:@"00%d.jpg",i+1]]];
}
return arr;
}
- (void)creatImageViewsOnScrollView
{
NSMutableArray *imgsArr = [self getImgsArray];
self.viewArr = [NSMutableArray array];
int i ;
for (i = 0; i < imgsArr.count; i ++) {
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(5, i*(kCellHeight+5), self.view.frame.size.width-10, kCellHeight)];
imgView.image = imgsArr[i];
[self.scrollView addSubview:imgView];
[self.viewArr addObject:imgView];
}
self.downY = i*(kCellHeight+5);
self.upY = 0;
self.startOffSet = 0;
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
logFunc;
self.startOffSet = scrollView.contentOffset.y;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y > 10000-kScreenHeight-kScreenHeight) {
CGSize size = self.scrollView.contentSize;
size.height = self.scrollView.contentSize.height+1000;
self.scrollView.contentSize = size;
}
if (scrollView.contentOffset.y > (scrollView.contentSize.height-kScreenHeight)&&scrollView.contentOffset.y > 0) {
偏移量超出屏幕范围
}else if (scrollView.contentOffset.y - self.startOffSet > 0){
UIImageView *imgView = [self.viewArr firstObject];
self.imageView.image = imgView.image;
//上拉
if (self.downY-scrollView.contentOffset.y < kScreenHeight+kCellHeight) {
[imgView removeFromSuperview];
[self.viewArr removeObjectAtIndex:0];
[self.viewArr addObject:imgView];
imgView.frame = CGRectMake(5, self.downY, kCellWidth, kCellHeight);
[self.scrollView addSubview:imgView];
self.downY = imgView.frame.origin.y+kCellHeight+5;
self.upY = ((UIImageView *)[self.viewArr firstObject]).frame.origin.y;
}
}else if (scrollView.contentOffset.y - self.startOffSet < 0){
UIImageView *imgView = [self.viewArr lastObject];
self.imageView.image = imgView.image;
///下拉
if (self.upY - scrollView.contentOffset.y > -kCellHeight) {
[imgView removeFromSuperview];
[self.viewArr removeObject:[self.viewArr lastObject]];
[self.viewArr insertObject:imgView atIndex:0];
imgView.frame = CGRectMake(5, self.upY, kCellWidth, kCellHeight);
[self.scrollView addSubview:imgView];
self.upY = imgView.frame.origin.y-kCellHeight-5;
self.downY = ((UIImageView*)[self.viewArr lastObject]).frame.origin.y;
}
}
}
-(UIScrollView *)scrollView
{
if (!_scrollView) {
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 10000);
_scrollView.delegate = self;
_scrollView.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.2];
[self.view addSubview:_scrollView];
}
return _scrollView;
}
-(UIImageView *)imageView
{
if (!_imageView) {
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth-60, 20, 60, 40)];
_imageView.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.2];
[self.view addSubview:_imageView];
}
return _imageView;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end