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

用三個 UIImageView 實作 ScrollView 無限循環輪播

編輯:iOS 開發基礎

用 scrollView 做循環展示,只有 1 到 10 張時還算簡單、開銷也小;但如果是 100 張、1000 張甚至更多,總不可能真的建立那麼多個 view。

這裡參考了一篇寫得很清楚的分享,核心思路是用 3 個 view 顯示無限多張圖片:透過 - (void)scrollViewDidEndDecelerating: 判斷目前滑到了第幾張,再據此調整這 3 個 view 的位置。我在這個基礎上做了些封裝。

自訂的 view 名為 EndlessLoopShowView:

#import

@class EndlessLoopShowView;

@protocol EndlessLoopShowViewDelegate

@optional
/* 滑動到第幾個位置 **/
- (void)endlessLoop:(EndlessLoopShowView *)showView scrollToIndex:(NSInteger)currentIndex;

@end

@interface EndlessLoopShowView : UIView

/* 圖片資料來源,可傳入 UIImage、圖片名稱或 URL,也可自行擴充 **/
@property (nonatomic, strong) NSArray * imageDataArr;

@property (nonatomic, weak) id delegate;

@end

實作部分:

#import "EndlessLoopShowView.h"

/* 無限循環的視圖 **/
@interface EndlessLoopShowView ()

@property (nonatomic, strong) UIScrollView * scrollView;
@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UIImageView *centerImageView;
@property (nonatomic, strong) UIImageView *rightImageView;

@property (nonatomic, assign) NSInteger currentIndex;  /* 當前滑到第幾張 **/
@property (nonatomic, assign) NSInteger imageCount;    /* 圖片總數 **/

@end

@implementation EndlessLoopShowView

#pragma mark - 生命週期
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    _currentIndex = -1;
    [self addSubview:self.scrollView];
    [self addImageViews];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    self.scrollView.frame = self.bounds;

    CGFloat imageW = CGRectGetWidth(self.scrollView.bounds);
    CGFloat imageH = CGRectGetHeight(self.scrollView.bounds);

    self.leftImageView.frame   = CGRectMake(imageW * 0, 0, imageW, imageH);
    self.centerImageView.frame = CGRectMake(imageW * 1, 0, imageW, imageH);
    self.rightImageView.frame  = CGRectMake(imageW * 2, 0, imageW, imageH);

    self.scrollView.contentSize = CGSizeMake(imageW * 3, 0);

    self.currentIndex = 0;
    [self setScrollViewContentOffsetCenter];
}

#pragma mark - set/get
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

/* 重寫 setter,切換時同步更新三張圖片 **/
- (void)setCurrentIndex:(NSInteger)currentIndex {
    if (_currentIndex != currentIndex) {
        _currentIndex = currentIndex;

        NSInteger leftImageIndex  = (currentIndex + _imageCount - 1) % _imageCount;
        NSInteger rightImageIndex = (currentIndex + 1) % _imageCount;

        _centerImageView.image = [UIImage imageNamed:self.imageDataArr[currentIndex]];
        _leftImageView.image   = [UIImage imageNamed:self.imageDataArr[leftImageIndex]];
        _rightImageView.image  = [UIImage imageNamed:self.imageDataArr[rightImageIndex]];

        [self setScrollViewContentOffsetCenter];

        if ([self.delegate respondsToSelector:@selector(endlessLoop:scrollToIndex:)]) {
            [self.delegate endlessLoop:self scrollToIndex:currentIndex];
        }
    }
}

#pragma mark - 加入三個 imageView
- (void)addImageViews {
    _leftImageView = [[UIImageView alloc] init];
    _leftImageView.contentMode = UIViewContentModeScaleAspectFit;
    [_scrollView addSubview:_leftImageView];

    _centerImageView = [[UIImageView alloc] init];
    _centerImageView.contentMode = UIViewContentModeScaleAspectFit;
    [_scrollView addSubview:_centerImageView];

    _rightImageView = [[UIImageView alloc] init];
    _rightImageView.contentMode = UIViewContentModeScaleAspectFit;
    [_scrollView addSubview:_rightImageView];
}

/* 把 scrollView 偏移回中心位置 **/
- (void)setScrollViewContentOffsetCenter {
    [self.scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.scrollView.bounds), 0) animated:NO];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGPoint offset = [_scrollView contentOffset];

    if (offset.x > CGRectGetWidth(scrollView.frame)) {        // 向右滑
        self.currentIndex = (self.currentIndex + 1) % _imageCount;
    } else if (offset.x < CGRectGetWidth(scrollView.frame)) { // 向左滑
        self.currentIndex = (self.currentIndex + _imageCount - 1) % _imageCount;
    }
}

#pragma mark - 公開方法
- (void)setImageDataArr:(NSArray *)imageDataArr {
    _imageDataArr = imageDataArr;
    self.imageCount = imageDataArr.count;
    self.currentIndex = 0;
}

@end

使用方式

- (void)viewDidLoad {
    [super viewDidLoad];

    EndlessLoopShowView * showView = [[EndlessLoopShowView alloc] initWithFrame:CGRectMake(0, 100, 200, 200)];
    showView.backgroundColor = [UIColor redColor];
    showView.imageDataArr = @[@"1", @"2", @"3", @"4", @"5"];
    showView.delegate = self;

    [self.view addSubview:showView];
}

- (void)endlessLoop:(EndlessLoopShowView *)showView scrollToIndex:(NSInteger)currentIndex {
    NSLog(@"currentIndex = %ld", currentIndex);
}

關鍵在於:currentIndex 的 setter 中一次更新左中右三張圖,並把 scrollView 的 contentOffset 拉回中間那一頁,於是無論滑多久,畫面上永遠只需要三個 imageView。索引以取餘運算遞增遞減,循環 therefore 自然成立。

文章轉自 Mr_Lee_ 的簡書

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