UIScrollView的终极复用

声明必要的属性

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

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

//滚动视图
@property (nonatomic, weak) UIScrollView * scrollView;

//所有的图片
@property (nonatomic, strong) NSArray * imageNames;

创建UIScrollView


    UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(self.imageNames.count*self.view.bounds.size.width, self.view.bounds.size.height);
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    
    //显示第一张图片
    [self showImageViewAtIndex:0];

显示第一张图片


//显示一个图片的view
- (void)showImageViewAtIndex:(NSInteger)index
{
    //先从复用池中找imageview
    UIImageView * imageView = [self.reusedImageViews anyObject];
    
    if (!imageView) {//没有就创建一个
        imageView = [[UIImageView alloc]init];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
    }else{//有的话就 把imageview移除
        [self.reusedImageViews removeObject:imageView];
    }
    
    //在这里可以配置imageview
    CGRect bounds = self.scrollView.bounds;
    CGRect imageViewFrame = bounds;
    
    imageViewFrame.origin.x = CGRectGetWidth(bounds) * index;
    imageView.tag = index;
    imageView.frame = imageViewFrame;
    imageView.image = [UIImage imageNamed:self.imageNames[index]];
    
    //把刚才从reusedImageViews移除的对象添加到visibleImageViews对象中
    [self.visibleImageViews addObject:imageView];
    
    //显示到scrollow上
    [self.scrollView addSubview:imageView];
}

滑动显示更多图片


#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self showImages];
}

- (void)showImages
{
    //获取当前显示范围内的图片的索引
    CGRect bounds = self.scrollView.bounds;
    CGFloat minX = CGRectGetMinX(bounds);
    CGFloat maxX = CGRectGetMaxX(bounds);
    CGFloat width = CGRectGetWidth(bounds);
    
    //第一个索引
    NSInteger firstIndex = (NSInteger)floor(minX / width);
    //最后一个索引
    NSInteger lastIndex = (NSInteger)floor(maxX / width);
    
    //处理越界情况
    if (firstIndex < 0) firstIndex = 0;
    if (lastIndex >= self.imageNames.count) lastIndex = self.imageNames.count - 1;
    
    
    //回收不再显示的imageview
    NSInteger imageViewIndex = 0;
    for (UIImageView * imageView in self.visibleImageViews)
    {
        imageViewIndex = imageView.tag;
        
        //不在显示范围内的
        if (imageViewIndex < firstIndex || imageViewIndex > lastIndex)
        {
            //添加到复用池中
            [self.reusedImageViews addObject:imageView];
            [imageView removeFromSuperview];
        }
    }
    
    //取出复用池中的图片
    [self.visibleImageViews minusSet:self.reusedImageViews];
    
    //是否需要显示心的视图
    for (NSInteger index = firstIndex; index <= lastIndex; index++)
    {
        BOOL isShow = NO;
        
        //遍历可见数据的对象
        for (UIImageView * imageView in self.visibleImageViews)
        {
            if (imageView.tag == index)
            {
                isShow = YES;
            }
        }
        
        if (!isShow)
        {
            [self showImageViewAtIndex:index];
        }
        
    }
    
}

你可能感兴趣的:(UIScrollView的终极复用)