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];
        }
        
    }
    
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,257評論 4 61
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,569評論 25 708
  • 窗外只有一盞燈亮著,窗臺里面咕嚕嚕響著的是一只水槽,水槽里的壩壘不斷下減,水亮的碗碟一層層起平房。十二個小時前,一...
    跛足游魚閱讀 289評論 0 1
  • 在中國歷史上,曾經涌現出了許多著名的女政治家,她們巾幗不讓須眉,為社會的穩定發展做出了杰出的貢獻,這其中影視劇...
    葉塞尼婭閱讀 1,797評論 0 3
  • 你有沒有像我這樣,一夜又一夜,眼淚把枕頭浸透 我想摸摸我的心臟,告訴它辛苦了,除了每秒的搏動,還得騰出空來抽搐
    27一十四閱讀 167評論 0 0