#import "ViewController.h"
#define ImageCount 10#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height ViewController ()<UIScrollViewDelegate>{ UIScrollView *_scrollView; UIImageView *_leftImageView; UIImageView *_middleImageView; UIImageView *_rightImageView; int currentImage; UIPageControl *_pageControl;} @implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; _scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds]; [self.view addSubview:_scrollView]; _scrollView.delegate = self; _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width*3, _scrollView.frame.size.height); //设置当前scrollView显示中间区域 _scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width, 0); _scrollView.pagingEnabled = YES; //添加imageView //左 _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; _leftImageView.contentMode = UIViewContentModeScaleAspectFill; [_scrollView addSubview:_leftImageView]; //右 _rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*2, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; _rightImageView.contentMode = UIViewContentModeScaleAspectFill; [_scrollView addSubview:_rightImageView]; //中 _middleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; _middleImageView.contentMode = UIViewContentModeScaleAspectFill; [_scrollView addSubview:_middleImageView]; currentImage = 0; //初始化pageControl _pageControl = [[UIPageControl alloc]init]; //根据页数返回pageControl合适的大小 CGSize size = [_pageControl sizeForNumberOfPages:ImageCount]; _pageControl.center = CGPointMake(SCREEN_WIDTH/2, 580); _pageControl.bounds = CGRectMake(0, 0, size.width, size.height);// _pageControl.backgroundColor = [UIColor whiteColor]; //设置颜色 _pageControl.pageIndicatorTintColor = [UIColor redColor]; _pageControl.currentPageIndicatorTintColor = [UIColor grayColor]; _pageControl.numberOfPages = ImageCount; _pageControl.currentPage = currentImage; [self.view addSubview:_pageControl]; [self setImage]; [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(changeImage) userInfo:nil repeats:YES]; }-(void)changeImage{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1.0f]; _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH*2, 0); [UIView commitAnimations]; currentImage = (currentImage+1)%ImageCount; _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0); [self setImage];}-(void) setImage{ _pageControl.currentPage = currentImage;// 加载默认图片 _leftImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",(ImageCount+currentImage-1)%ImageCount]]; _rightImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",(currentImage+1)%ImageCount]]; _middleImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",currentImage]];}-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ //如果向右滑动 if (scrollView.contentOffset.x == SCREEN_WIDTH*2) { currentImage = (currentImage+1)%ImageCount; scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0); [self setImage]; } //如果向左滑动 else if(scrollView.contentOffset.x == 0) { currentImage = (ImageCount+currentImage-1)%ImageCount; scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0); [self setImage]; }}