三个imageView搞定左右轮播图
2016-06-24 本文已影响239人
文刂阿毛
初始化scrollView让中间的imageView显示 照片数组的第1张
左边imageView显示照片数组的最后一张
右边imageView显示照片数组的第二张
//滚动视图懒加载
- (UIScrollView *)scrollView {
if (!_scrollView) {
// 开始创建滚动视图及属性
_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) {
//创建两个图片
_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;
//给数组赋值
self.imageArray = imageArray;
self.leftIndex = self.imageArray.count - 1;
//设置视图
[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协议---
- (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);
//左边照片比右边照片在数组中的下标少2
self.leftIndex = self.rightIndex - 2;
//往右偏移时左右imageView 的image 都要随时变更
//当左边照片下标为-1时,代表的是:到数组最后一张照片
if (self.leftIndex == -1) {
self.leftIndex = self.imageArray.count - 1;
}else if (self.leftIndex == -2) {//当左边照片下标为-2时,代表的是:到数组倒数第二张照片
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];
//右边照片比左边照片在数组中的下标多2
self.rightIndex = self.leftIndex + 2;
//往左偏移时左右imageView 的image 都要随时变更
//当右边照片下标为数组个数时,代表的是:到数组第一张照片
if (self.rightIndex == self.imageArray.count) {
self.rightIndex = 0;
}else if (self.rightIndex == self.imageArray.count + 1) {//当右边照片下标为数组个数 + 1时,代表的是:到数组第二张照片
self.rightIndex = 1;
}
self.rightImageView.image = self.imageArray[self.rightIndex];
NSLog(@"--右边照片----%ld", self.rightIndex);
}
}