#import "ViewController.h" #define IMAGENUMBER 5
#define SIZE self.view.bounds.size
@interface ViewController ()
{
UIScrollView *_scrollerView ;
UIImageView *_leftImageView ;
UIImageView *_centerImageVIew;
UIImageView *_rightImagView;
UIPageControl *_pageCoontroller; //当前索引下标
NSInteger _curremtIndex; }
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor =[UIColor brownColor];
[self createScrollerView];
} -(void)createScrollerView
{
_scrollerView =[[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; [self.view addSubview:_scrollerView]; //创建左,中,右,三个ImageView视图
_leftImageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"0.jpg"]];
_centerImageVIew =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
_rightImagView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.jpg"]]; _leftImageView.frame =CGRectMake(, , SIZE.width, SIZE.height);
_centerImageVIew.frame =CGRectMake(SIZE.width, , SIZE.width, SIZE.height);
_rightImagView.frame =CGRectMake(SIZE.width*, , SIZE.width, SIZE.height); //添加
[_scrollerView addSubview:_leftImageView];
[_scrollerView addSubview:_centerImageVIew];
[_scrollerView addSubview:_rightImagView];
_scrollerView.contentSize=CGSizeMake(SIZE.width*, SIZE.height);
_scrollerView.pagingEnabled=YES;
_scrollerView.delegate=self;
_scrollerView.contentOffset=CGPointMake(SIZE.width, ); _scrollerView.showsHorizontalScrollIndicator=NO;
_scrollerView.showsVerticalScrollIndicator=NO; //页码指示器
_pageCoontroller =[[UIPageControl alloc]initWithFrame:CGRectMake((SIZE.width-)/, SIZE.height-, SIZE.width-, )]; _curremtIndex=;
_pageCoontroller.numberOfPages=IMAGENUMBER;
_pageCoontroller.backgroundColor=[UIColor clearColor];
_pageCoontroller.pageIndicatorTintColor=[UIColor orangeColor];
_pageCoontroller.currentPageIndicatorTintColor=[UIColor purpleColor];
_pageCoontroller.currentPage=_curremtIndex;
[_pageCoontroller addTarget:self action:@selector(NextImageClicked) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_pageCoontroller];
} -(void)NextImageClicked
{
NSInteger pageImage =_pageCoontroller.currentPage;
if (pageImage==_pageCoontroller.currentPage+) {
pageImage+=;
}else{
pageImage-=;
}
_curremtIndex=pageImage;
_scrollerView.contentOffset=CGPointMake(SIZE.width*pageImage, );
} -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self loadImage];
[UIView animateWithDuration:0.5 animations:^{
_pageCoontroller.currentPage= _curremtIndex;
}];
} -(void)loadImage
{
//创建动画效果
CATransition *transaction =[[CATransition alloc]init]; CGPoint offset =_scrollerView.contentOffset; //向右滚动
if (offset.x>) { //动画类型
transaction.type =@"fade"; //通过取余运算获取当前索引下标
_curremtIndex=(_curremtIndex+)%IMAGENUMBER;
NSString *rightImage =[NSString stringWithFormat:@"%li.jpg",_curremtIndex];
_rightImagView.image=[UIImage imageNamed:rightImage];
}else{//向左滚动
transaction.type=@"fade";
_curremtIndex=(_curremtIndex-+IMAGENUMBER)%IMAGENUMBER;
NSString *leftImage =[NSString stringWithFormat:@"%li.jpg",_curremtIndex];
_leftImageView.image=[UIImage imageNamed:leftImage];
} //添加动画效果
[_scrollerView.layer addAnimation:transaction forKey:@"transaction"];
}