您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS UIScrollView 無限循環滾動:三種思路與完整代碼

編輯:iOS 開發綜合

輪播的無限循環有幾種常見做法,各有取捨。以下整理三種思路,前兩種附完整程式碼。

三種思路對比

第一種:依結束位置重置 contentOffset
原理是在滾動結束時把偏移量拉回中間區段,製造視覺上的循環。優點是程式碼容易理解,缺點是會建立較多 imageView,佔用額外記憶體。

第二種:雙 View 緩衝重用
只用「當前 View」與「緩衝 View」兩個物件,把當前 View 放在中間,滑動時優先更新緩衝 View 的內容,滑動結束再更新當前 View。記憶體消耗小,程式碼稍複雜一些。

第三種:改用 UICollectionView
一次性把資料源擴充成 images.count * 1000,載入時跳到 (images.count * 1000) / 2 + ((images.count * 1000) / 2) % images.count 的位置即可。程式碼最精簡,本文不展開。

完整專案可參考:UIScrollViewLoopDemo

做法一:重置 contentOffset

/**
 * 第一種方式:利用結束的位置重新設置 contentOffset,產生視覺上的無限循環
 * 優點:代碼易懂 缺點:會建立多餘的內存
 */
- (void)realizeScrollLoop1 {
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    scrollView.frame = self.view.bounds;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;

    [scrollView setContentSize:CGSizeMake(([self.slideImages count] + 2) * scrollView.frame.size.width, 0)];
    CGSize scrollViewSize = scrollView.frame.size;

    // 遍歷建立子控件
    [self.slideImages enumerateObjectsUsingBlock:^(NSString *imageName, NSUInteger idx, BOOL *stop) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:imageName];
        imageView.frame = CGRectMake((idx + 1) * scrollViewSize.width, 0,
                                     scrollViewSize.width, scrollViewSize.height);
        [scrollView addSubview:imageView];
    }];

    // 把最後一張圖放到第一張的位置
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:self.slideImages[[self.slideImages count] - 1]];
    imageView.frame = CGRectMake(0, 0, scrollViewSize.width, scrollViewSize.height);
    [scrollView addSubview:imageView];

    // 把第一張圖放到最後,形成視覺循環
    UIImageView *lastImageView = [[UIImageView alloc] init];
    lastImageView.image = [UIImage imageNamed:self.slideImages[0]];
    lastImageView.frame = CGRectMake(scrollViewSize.width * ([self.slideImages count] + 1), 0,
                                     scrollViewSize.width, scrollViewSize.height);
    [scrollView addSubview:lastImageView];

    [scrollView setContentOffset:CGPointMake(scrollViewSize.width, 0)];
}

對應的 delegate:滑到頭尾時無縫跳回中間。

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger page = scrollView.contentOffset.x / scrollView.frame.size.width;

    // 當前是第 0 頁,跳到資料中最後一張的位置
    if (page == 0) {
        [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width * [[self slideImages] count], 0)];
    } else if (page == [[self slideImages] count] + 1) {
        // 是最後一頁,跳到第一張的位置
        [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0)];
    }
}

做法二:帶緩衝重用的版本

/**
 * 第二種方式:以兩個變數分別代表當前 View 與緩衝 View,最多只建立三個 View
 * 優點:內存消耗少 缺點:代碼稍複雜
 */
- (void)realizeScrollLoop2 {
    status = ScrollViewLoopStatusResuing;

    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.pagingEnabled = YES;
    scrollView.frame = self.view.bounds;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;

    CGSize scrollViewSize = scrollView.frame.size;
    scrollView.contentSize = CGSizeMake(3 * scrollViewSize.width, 0);
    scrollView.contentOffset = CGPointMake(scrollViewSize.width, 0);

    UIImageView *currentView = [[UIImageView alloc] init];
    currentView.tag = 0;
    currentView.frame = CGRectMake(scrollViewSize.width, 0,
                                   scrollViewSize.width, scrollViewSize.height);
    currentView.image = [UIImage imageNamed:@"00.jpg"];
    [scrollView addSubview:currentView];
    self.currentView = currentView;

    [self resuingView];
    self.index = 0;
}

滑動過程中即時更新緩衝 View 的內容與位置:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (status == ScrollViewLoopStatusResuing) {
        if (scrollView.contentOffset.x > _currentView.frame.origin.x) {
            NSInteger val = self.index + 1;
            if (self.index >= [self.slideImages count] - 1) {
                val = 0;
            }
            self.resuingView.image = [UIImage imageNamed:[NSString stringWithFormat:@"0%zd.jpg", val]];
            self.resuingView.x = CGRectGetMinX(_currentView.frame) + _currentView.width;
            self.isLastScrollDirection = YES;
        } else {
            NSInteger val = self.index - 1;
            if (val < 0) {
                val = [self.slideImages count] - 1;
            }
            self.resuingView.image = [UIImage imageNamed:[NSString stringWithFormat:@"0%zd.jpg", val]];
            self.resuingView.x = 0;
            self.isLastScrollDirection = NO;
        }
    }
}

滑動結束後,把當前 View 的圖片更新為最新頁:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (self.isLastScrollDirection) {
        self.index++;
    } else {
        self.index--;
    }

    // 邊界補全
    if (self.index < 0) {
        self.index = [self.slideImages count] - 1;
    } else if (self.index > [self.slideImages count] - 1) {
        self.index = 0;
    }

    _currentView.image = [UIImage imageNamed:[NSString stringWithFormat:@"0%zd.jpg", self.index]];
}

兩種做法都能達成無限循環。若圖片數量不多、規格固定,做法一寫起來最省心;若圖片尺寸大或來源是網路,做法二能明顯降低記憶體峰值。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved