iOS--UIScrollView图片动画切换【实现每次只加载3张图片,进而减少占用内存,可循环滚动】
#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(0, 20, SIZE.width, SIZE.height); _centerImageVIew.frame =CGRectMake(SIZE.width, 20, SIZE.width, SIZE.height); _rightImagView.frame =CGRectMake(SIZE.width*2, 20, SIZE.width, SIZE.height); //添加 [_scrollerView addSubview:_leftImageView]; [_scrollerView addSubview:_centerImageVIew]; [_scrollerView addSubview:_rightImagView]; _scrollerView.contentSize=CGSizeMake(SIZE.width*3, SIZE.height); _scrollerView.pagingEnabled=YES; _scrollerView.delegate=self; _scrollerView.contentOffset=CGPointMake(SIZE.width, 0); _scrollerView.showsHorizontalScrollIndicator=NO; _scrollerView.showsVerticalScrollIndicator=NO; //页码指示器 _pageCoontroller =[[UIPageControl alloc]initWithFrame:CGRectMake((SIZE.width-300)/2, SIZE.height-10, SIZE.width-50, 10)]; _curremtIndex=0; _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+1) { pageImage+=1; }else{ pageImage-=1; } _curremtIndex=pageImage; _scrollerView.contentOffset=CGPointMake(SIZE.width*pageImage, 0); } -(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>300) { //动画类型 transaction.type =@"fade"; //通过取余运算获取当前索引下标 _curremtIndex=(_curremtIndex+1)%IMAGENUMBER; NSString *rightImage =[NSString stringWithFormat:@"%li.jpg",_curremtIndex]; _rightImagView.image=[UIImage imageNamed:rightImage]; }else{//向左滚动 transaction.type=@"fade"; _curremtIndex=(_curremtIndex-1+IMAGENUMBER)%IMAGENUMBER; NSString *leftImage =[NSString stringWithFormat:@"%li.jpg",_curremtIndex]; _leftImageView.image=[UIImage imageNamed:leftImage]; } //添加动画效果 [_scrollerView.layer addAnimation:transaction forKey:@"transaction"]; }
作者:青卷三部
签名:灾难总是接踵而至,这正是世间的常理。只要找个理由,就会有谁来救你吗?要是死了,就只是说明我不过是如此程度的男人。