码迷,mamicode.com
首页 > 其他好文 > 详细

UI_UIScrollView图片动画切换【实现每次只加载3张图片,进而减少占用内存,可循环滚动】

时间:2015-10-09 00:26:26      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

#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"];
}

UI_UIScrollView图片动画切换【实现每次只加载3张图片,进而减少占用内存,可循环滚动】

标签:

原文地址:http://www.cnblogs.com/LQCQ-Silent/p/4862619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!