码迷,mamicode.com
首页 > 编程语言 > 详细

滚动ScrollView---数组存放图片,设置3个滚动视图,用来滑动多张图片

时间:2015-06-01 22:46:09      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:分页   uiscrollview   imageview   

问题描述:循环滑动切换n多张图片,不事先加载完成,现用现取。

解题思路:将要显示的图片存放在数组中,设置3个滚动视图,左边视图,正在显示的视图,右边视图

实现代码如下:



//  ViewController.m


#import "ViewController.h"


#define kWith self.view.frame.size.width

#define kHeight self.view.frame.size.height


@interface ViewController () <UIScrollViewDelegate>


//图片名称数组

@property(nonatomic,strong)NSMutableArray * imageNameArray;


//图片视图缓冲池数组

@property(nonatomic,strong)NSMutableArray * imageArray;


//加载滚动视图

@property(nonatomic,weak) UIScrollView * scrollView;


@end




@implementation ViewController


#pragma mark - 懒加载 imageNameArray

- (NSMutableArray *)imageNameArray

{

    if(_imageNameArray==nil)

    {

        _imageNameArray=[NSMutableArray array];

    }

    return _imageNameArray;

}


#pragma mark - 懒加载 imageArray

- (NSMutableArray *)imageArray

{

    if(_imageArray==nil)

    {

        _imageArray=[NSMutableArray array];

    }

    return _imageArray;

}


#pragma mark - 入口

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //加载数据

    [self _loadData];

    

    //加载子视图

    [self _loadSubViews];

}


#pragma mark - 加载数据---将图片名存入数组

- (void)_loadData

{

    for (int i=0; i<11; i++)

    {

        NSString * name=[NSString stringWithFormat:@"t%d.jpg",i+1];

        [self.imageNameArray addObject:name];   //添加到存放图片名的数组

        //NSLog(@"%@",self.imageNameArray);     //测试代码

    }

}


#pragma mark - 加载子视图

- (void)_loadSubViews

{

    //定义滚动视图

    UIScrollView * scroll=[[UIScrollView alloc]initWithFrame:self.view.bounds];

    

    //添加三个可滚动的imageView视图 视图总长度为:3*kWith   其中i*kWith代表每个imageView的起始x

    for (int i=0; i<3; i++)

    {

        UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake(i*kWith, 0, kWith, kHeight)];

        imageView.image=[UIImage imageNamed:self.imageNameArray[i]];

        [self.imageArray addObject:imageView];    //添加到存放图片的数组

        [scroll addSubview:imageView];            //添加到滚动视图

    }

    

    [self.view addSubview:scroll];    //添加到显示视图

    

    //设置滚动视图的属性

    scroll.delegate=self;    //设置代理

    scroll.contentSize=CGSizeMake(self.imageNameArray.count*kWith, 0);    //图片连在一起的总长度 self.imageNameArray.count*kWith

    scroll.pagingEnabled=YES;    //分页显示

    scroll.showsHorizontalScrollIndicator=NO;    //隐藏下面的滑动进度条

    self.scrollView=scroll;

}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    if (scrollView.contentOffset.x/kWith == (int)(scrollView.contentOffset.x/kWith))

    {

        NSMutableArray * temArray=[self getUnusedViewWith:scrollView.contentOffset.x];

        if (scrollView.contentOffset.x>0 && scrollView.contentOffset.x<kWith*(self.imageNameArray.count-1))

        {

            //设置左边的视图

            [self setImageFrameWithImage:temArray[0] andImageWith:-1];

            

            //设置右边的视图

            [self setImageFrameWithImage:temArray[1] andImageWith:1];

            

            //如果可利用的视图多了一个,说明中间是空白的

            if (temArray.count>2)

            {

                [self setImageFrameWithImage:temArray[2] andImageWith:0];

            }

        }

        else if(scrollView.contentOffset.x==0)

        {

            [self setImageFrameWithImage:self.imageArray[0] andImageWith:0];

            [self setImageFrameWithImage:self.imageArray[1] andImageWith:1];

        }

        else

        {

            [self setImageFrameWithImage:self.imageArray[0] andImageWith:0];

            [self setImageFrameWithImage:self.imageArray[1] andImageWith:-1];

        }

    }

}


#pragma mark - UIScrollViewDelegate代理需实现的方法---方法二

/*

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    //NSLog(@"======");   //测试代码---看是否每滑动一张图片就走一次这个方法

    

    //定义一个(NSMutableArray *)类型的对象来接收  获取未使用的视图对象

    NSMutableArray * temArray=[self getUnusedViewWith:scrollView.contentOffset.x];

    if (scrollView.contentOffset.x>0 && scrollView.contentOffset.x<kWith*(self.imageNameArray.count-1))

    {

        //设置左边的视图

        [self setImageFrameWithImage:temArray[0] andImageWith:-1];

        

        //设置右边的视图

        [self setImageFrameWithImage:temArray[1] andImageWith:1];

        

        //如果可利用的视图多了一个,说明中间是空白的

        if (temArray.count>2)

        {

            [self setImageFrameWithImage:temArray[2] andImageWith:0];

        }

        //如果是0 说明滑到最左边去了


    }

    else if(scrollView.contentOffset.x==0)

    {

        [self setImageFrameWithImage:self.imageArray[0] andImageWith:0];

        [self setImageFrameWithImage:self.imageArray[1] andImageWith:1];

    }

    else if(scrollView.contentOffset.x==kWith*(self.imageNameArray.count-1))

    {

        [self setImageFrameWithImage:self.imageArray[0] andImageWith:0];

        [self setImageFrameWithImage:self.imageArray[1] andImageWith:-1];

    }

}*/


#pragma mark - 设置imageView的属性

- (void) setImageFrameWithImage: (UIImageView *)image andImageWith: (NSInteger) flag

{

    //设置frame

    CGFloat space=flag*kWith;

    

    CGFloat X=self.scrollView.contentOffset.x+space;

    image.frame=CGRectMake(X, 0, kWith, kHeight);

    

    //设置图片

    NSInteger imageIndex=image.frame.origin.x/kWith;

    

    NSLog(@"%g====%ld====%li====%ld",self.scrollView.contentOffset.x,imageIndex,flag,self.imageNameArray.count);

    

    image.image=[UIImage imageNamed:self.imageNameArray[imageIndex]];

    

}



#pragma mark - 获取未使用的视图对象

- (NSMutableArray *) getUnusedViewWith: (CGFloat) offX

{

    NSMutableArray * temArray=[NSMutableArray array];

    for (int i=0; i<self.imageArray.count; i++)

    {

        UIImageView * imageView=self.imageArray[i];

        

        //如果X的值和当前的滚动视图不相同则加入到可利用的数组

        if (imageView.frame.origin.x!=offX)

        {

            [temArray addObject:imageView];

        }

    }

    return temArray;

}


@end



  

  PS:最近在追一部很好看的小说《我和你差之微毫的世界》,作者很懒,不能做到按时更新,所以我常常很急躁的抓心挠肺地盼着作者快点更,现身说法,自己有没有做到按时整理-消化-吸收自己所学的知识呢,又有好几天没有发博客了,罪过啊~以后不管多忙都要按时更新博客,最少两天一更。说到做到!!!

  PPS:今天是六一,不管你是小朋友还是大朋友,都要有一颗童心啊~祝六一节快乐~~~



滚动ScrollView---数组存放图片,设置3个滚动视图,用来滑动多张图片

标签:分页   uiscrollview   imageview   

原文地址:http://blog.csdn.net/qq_27364431/article/details/46315409

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