#define kmainScreenW [UIScreen mainScreen].bounds.size.width#define kmainScreenH [UIScreen mainScreen].bounds.size.height#import "ViewController.h"@interface ViewController ()@property (nonatomic ,weak) UIScrollView *scrollView;
@property (nonatomic ,weak) UIImageView *imageViewLeft;
@property (nonatomic ,weak) UIImageView *imageViewCenter;
@property (nonatomic ,weak) UIImageView *imageViewRight;
@property (nonatomic ,strong) NSArray *dataArray;
@property (nonatomic ,weak) UIPageControl *pageControl;
@property (nonatomic ,assign) NSInteger currentImageIndex;
@property (nonatomic ,weak) NSTimer *timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *dataArray = @[@"image1",@"image2",@"image3",@"image4"];
_dataArray = dataArray;
_currentImageIndex = 0;
[self setUpScrollView];
[self setUpImageView];
if (dataArray.count >= 2) {
[self setUpPageConrol];
[self setUpTimer];
}
}
//添加ScrollView
- (void)setUpScrollView{
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.frame = CGRectMake(0, 0, kmainScreenW, kmainScreenW *0.6);
// scrollView.backgroundColor = [UIColor blueColor];
[self.view addSubview:scrollView];
scrollView.contentSize = CGSizeMake(_dataArray.count *kmainScreenW, kmainScreenW * 0.6);
scrollView.delegate = self;
[scrollView setContentOffset:CGPointMake(kmainScreenW, 0) animated:NO];
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
_scrollView = scrollView;
}
//添加ImageView
- (void)setUpImageView{
if (_dataArray.count >= 2) {
UIImageView *imageViewLeft = [[UIImageView alloc] init];
imageViewLeft.frame = CGRectMake(0, 0, kmainScreenW, kmainScreenW *0.6);
[_scrollView addSubview:imageViewLeft];
imageViewLeft.image = [UIImage imageNamed:_dataArray[_dataArray.count-1]];
_imageViewLeft = imageViewLeft;
}
UIImageView *imageViewCenter = [[UIImageView alloc] init];
imageViewCenter.frame = CGRectMake(kmainScreenW, 0, kmainScreenW, kmainScreenW *0.6);
[_scrollView addSubview:imageViewCenter];
if (_dataArray.count >= 1) {
imageViewCenter.image = [UIImage imageNamed:_dataArray[0]];
}
// imageViewCenter.backgroundColor = [UIColor redColor];
_imageViewCenter = imageViewCenter;
if (_dataArray.count >= 2) {
UIImageView *imageViewRight = [[UIImageView alloc] init];
imageViewRight.frame = CGRectMake(kmainScreenW *2.0, 0, kmainScreenW, kmainScreenW *0.6);
[_scrollView addSubview:imageViewRight];
imageViewRight.image = [UIImage imageNamed:_dataArray[1]];
_imageViewRight = imageViewRight;
}
}
//添加PageConrol
- (void)setUpPageConrol{
UIPageControl *pageControl = [[UIPageControl alloc] init];
CGSize size = [pageControl sizeForNumberOfPages:_dataArray.count];
pageControl.bounds = CGRectMake(0, 0, size.width, size.height);
pageControl.center = CGPointMake(kmainScreenW *0.5, size.height);
pageControl.pageIndicatorTintColor = [UIColor whiteColor];
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
pageControl.numberOfPages = _dataArray.count;
_pageControl = pageControl;
[self.view addSubview:pageControl];
}
// 添加定时器
- (void)setUpTimer{
NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(timerChanged) userInfo:nil repeats:YES];
self.timer = timer;
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
//定时器生效,图片自动轮播
- (void)timerChanged{
NSInteger page = (_pageControl.currentPage + 1)%_dataArray.count;
_pageControl.currentPage = page;
[self pageChanged:_pageControl];
}
//页面变动
- (void)pageChanged: (UIPageControl *)pageControl{
CGFloat x = 2 * kmainScreenW;
// [_scrollView setContentOffset:CGPointMake(x, 0) animated:YES];
// _currentImageIndex = pageControl.currentPage;
// [self scrollViewDidEndDecelerating:_scrollView];
NSInteger leftImageIndex,rightImageIndex;
CGPoint offset = [_scrollView contentOffset];
_currentImageIndex = (_currentImageIndex + 1) %_dataArray.count;
leftImageIndex = (_currentImageIndex - 1 + _dataArray.count)%_dataArray.count;
rightImageIndex = (_currentImageIndex + 1)%_dataArray.count;
_imageViewCenter.image = [UIImage imageNamed:_dataArray[_currentImageIndex]];
_imageViewLeft.image = [UIImage imageNamed:_dataArray[leftImageIndex]];
_imageViewRight.image = [UIImage imageNamed:_dataArray[rightImageIndex]];
_pageControl.currentPage = _currentImageIndex;
[UIView animateWithDuration:1.0 animations:^{
_scrollView.contentOffset = CGPointMake(2 * kmainScreenW,0);
}];
_scrollView.contentOffset = CGPointMake(kmainScreenW,0);
}
//滑动开始的时候停止定时器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[_timer invalidate];
}
//滑动完毕的时候启动定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[self setUpTimer];
}
//设置无限循环播放
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
[self reloadImage];
[_scrollView setContentOffset:CGPointMake(kmainScreenW, 0) animated:NO];
// _scrollView.contentOffset = CGPointMake(kmainScreenW,0);//两个方法都可以
_pageControl.currentPage = _currentImageIndex;
}
//重新加载图片
- (void)reloadImage{
NSInteger leftImageIndex,rightImageIndex;
CGPoint offset = [_scrollView contentOffset];
if (offset.x >kmainScreenW) {//向右滑动
_currentImageIndex = (_currentImageIndex + 1) %_dataArray.count;
}else if(offset.x < kmainScreenW){//向左滑动
_currentImageIndex = (_currentImageIndex - 1 + _dataArray.count) %_dataArray.count;
}
leftImageIndex = (_currentImageIndex - 1 + _dataArray.count)%_dataArray.count;
rightImageIndex = (_currentImageIndex + 1)%_dataArray.count;
_imageViewCenter.image = [UIImage imageNamed:_dataArray[_currentImageIndex]];
_imageViewLeft.image = [UIImage imageNamed:_dataArray[leftImageIndex]];
_imageViewRight.image = [UIImage imageNamed:_dataArray[rightImageIndex]];
}
@end