初始化scrollView讓中間的imageView顯示 照片數(shù)組的第1張
左邊imageView顯示照片數(shù)組的最后一張
右邊imageView顯示照片數(shù)組的第二張
//滾動(dòng)視圖懶加載
- (UIScrollView *)scrollView {
if (!_scrollView) {
// 開(kāi)始創(chuàng)建滾動(dòng)視圖及屬性
_scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
_scrollView.contentSize = CGSizeMake(kWidth * self.imageArray.count, kHeight);
_scrollView.delegate = self;
_scrollView.pagingEnabled = YES;
_scrollView.bounces = NO;
_scrollView.bouncesZoom = NO;
_scrollView.showsHorizontalScrollIndicator = NO;
#warning 初始的scrollView的偏移量
_scrollView.contentOffset = CGPointMake(kWidth, 0);
}
return _scrollView;
}
//左邊視圖懶加載
- (UIImageView *)leftImageView {
if (!_leftImageView) {
//創(chuàng)建兩個(gè)圖片
_leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
_leftImageView.image = self.imageArray[self.imageArray.count - 1];
}
return _leftImageView;
}
//右邊視圖懶加載
- (UIImageView *)rightImageView {
if (!_rightImageView) {
_rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth * 2, 0, kWidth, kHeight)];
_rightImageView.image = self.imageArray[1];
}
return _rightImageView;
}
- (UIImageView *)moddileImage {
if (!_moddileImage) {
_moddileImage = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth , 0, kWidth, kHeight)];
_moddileImage.image = self.imageArray[0];
}
return _moddileImage;
}
//初始化方法
- (instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray
{
self = [super initWithFrame:frame];
if (self) {
self.rightIndex = 1;
//給數(shù)組賦值
self.imageArray = imageArray;
self.leftIndex = self.imageArray.count - 1;
//設(shè)置視圖
[self creatScrollView];
}
return self;
}
- (void)creatScrollView {
[self startTimer];
[self.scrollView addSubview:self.leftImageView];
[self.scrollView addSubview:self.rightImageView];
[self.scrollView addSubview:self.moddileImage];
[self addSubview:self.scrollView];
[self addSubview:self.pageControl];
}
核心代碼片段
#pragma mark ---scrollView協(xié)議---
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//#define kWidth self.frame.size.width
//scrollView的偏移量為kWidth * 2 代表的是往右偏移
if (scrollView.contentOffset.x == kWidth * 2) {
scrollView.contentOffset = CGPointMake(kWidth, 0);
self.pageControl.currentPage = self.rightIndex;
self.rightIndex++;
if (self.rightIndex == self.imageArray.count) {
self.rightIndex = 0;
}
self.moddileImage.image = self.rightImageView.image;
self.rightImageView.image = self.imageArray[self.rightIndex];
NSLog(@"--右邊照片----%ld", self.rightIndex);
//左邊照片比右邊照片在數(shù)組中的下標(biāo)少2
self.leftIndex = self.rightIndex - 2;
//往右偏移時(shí)左右imageView 的image 都要隨時(shí)變更
//當(dāng)左邊照片下標(biāo)為-1時(shí)处面,代表的是:到數(shù)組最后一張照片
if (self.leftIndex == -1) {
self.leftIndex = self.imageArray.count - 1;
}else if (self.leftIndex == -2) {//當(dāng)左邊照片下標(biāo)為-2時(shí)厂置,代表的是:到數(shù)組倒數(shù)第二張照片
self.leftIndex = self.imageArray.count - 2;
}
self.leftImageView.image = self.imageArray[self.leftIndex];
NSLog(@"----左邊照片----%ld", self.leftIndex);
}
//scrollView的偏移量為0 代表的是往左偏移
if (scrollView.contentOffset.x == 0) {
scrollView.contentOffset = CGPointMake(kWidth, 0);
self.pageControl.currentPage = self.leftIndex;
self.leftIndex--;
if (self.leftIndex < 0) {
self.leftIndex = self.imageArray.count - 1;
}
NSLog(@"----左邊照片----%ld", self.leftIndex);
self.moddileImage.image = self.leftImageView.image;
self.leftImageView.image = self.imageArray[self.leftIndex];
//右邊照片比左邊照片在數(shù)組中的下標(biāo)多2
self.rightIndex = self.leftIndex + 2;
//往左偏移時(shí)左右imageView 的image 都要隨時(shí)變更
//當(dāng)右邊照片下標(biāo)為數(shù)組個(gè)數(shù)時(shí),代表的是:到數(shù)組第一張照片
if (self.rightIndex == self.imageArray.count) {
self.rightIndex = 0;
}else if (self.rightIndex == self.imageArray.count + 1) {//當(dāng)右邊照片下標(biāo)為數(shù)組個(gè)數(shù) + 1時(shí)魂角,代表的是:到數(shù)組第二張照片
self.rightIndex = 1;
}
self.rightImageView.image = self.imageArray[self.rightIndex];
NSLog(@"--右邊照片----%ld", self.rightIndex);
}
}