3个UIimageView实现图片的循环切换
2015-06-03 14:21
471 查看
#import "ViewController.h"
#define KWidth self.view.frame.size.width
#define KHeight self.view.frame.size.height
@interface
ViewController ()<UIScrollViewDelegate>
@property(nonatomic,weak)
UIScrollView *scroll;
/**存放图片名的数组*/
@property(nonatomic,strong)
NSMutableArray *imageNameArray;
/**存放3个UIimageView的数组*/
@property(nonatomic,strong)
NSMutableArray *imageViewArray;
/**存放3个UIimageView中未被利用的UIimageView的数组*/
@property(nonatomic,strong)
NSMutableArray *imageUnusedViewArray;
@end
@implementation ViewController
//懒加载(用到时才加载)
-(NSMutableArray *)imageNameArray{
if (_imageNameArray==nil)
{
_imageNameArray = [NSMutableArray
array];
}
return
_imageNameArray;
}
-(NSMutableArray *)imageViewArray{
if (_imageViewArray==nil)
{
_imageViewArray = [NSMutableArray
array];
}
return
_imageViewArray;
}
-(NSMutableArray *)imageUnusedViewArray{
if (_imageUnusedViewArray==nil)
{
_imageUnusedViewArray = [NSMutableArray
array];
}
return
_imageUnusedViewArray;
}
- (void)viewDidLoad {
[super
viewDidLoad];
//加载数据
[self
_loadData];
//加载视图
[self
_loadView];
}
#pragma mark - 加载数据
-(void)_loadData{
for (int i=1; i<12; i++)
{
NSString *imageName = [NSString
stringWithFormat:@"t%i.jpg",i];
[self.imageNameArray
addObject:imageName];
}
}
#pragma mark - 加载视图
-(void)_loadView{
UIScrollView *scroll = [[UIScrollView
alloc]
initWithFrame:self.view.bounds];
[self.view
addSubview:scroll];
scroll.contentSize =
CGSizeMake(KWidth*self.imageNameArray.count,
0);
scroll.pagingEnabled =
YES;
scroll.delegate =
self;
self.scroll = scroll;
//添加三个视图
for (int i=0; i<3; i++)
{
UIImageView *imageView = [[UIImageView
alloc] initWithFrame:CGRectMake(i*KWidth,
0, KWidth,
KHeight)];
imageView.image = [UIImage
imageNamed:self.imageNameArray[i]];
[self.scroll
addSubview:imageView];
[self.imageViewArray
addObject:imageView];
}
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//只有contentOffset是整数时才进行下面判断,这样做就相当于-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView方法
if (scrollView.contentOffset.x/KWidth==(int)(scrollView.contentOffset.x/KWidth))
{
//获取未利用的imageView
[self
getUnusedView];
//不是在第一页,也不是在最后一页的时候
if (scrollView.contentOffset.x>0&&scrollView.contentOffset.x<KWidth*(self.imageNameArray.count-1))
{
[self
setImageFrameWithImage:self.imageUnusedViewArray[0]
andImageWith:frontView];
[self
setImageFrameWithImage:self.imageUnusedViewArray[1]
andImageWith:nextView];
//如果当没利用的imageView数量大于3时,证明当前页面也没有利用,所以当前页面就没有图片(当滑的太快时就会出现以上情况)
if (self.imageUnusedViewArray.count>2)
{
[self
setImageFrameWithImage:self.imageUnusedViewArray[2]
andImageWith:defineView];
}
}
//如果实在第一页
else if(scrollView.contentOffset.x==0)
{
[self
setImageFrameWithImage:self.imageViewArray[0]
andImageWith:defineView];
[self
setImageFrameWithImage:self.imageViewArray[1]
andImageWith:nextView];
}
//如果是在最后一页
else
{
[self
setImageFrameWithImage:self.imageViewArray[0]
andImageWith:defineView];
[self
setImageFrameWithImage:self.imageViewArray[1]
andImageWith:frontView];
}
}
//如果contentOffset不是整数就返回
else
{
return;
}
}
#pragma mark - 设置frame和图片
-(void)setImageFrameWithImage:(UIImageView *)imageView andImageWith:(NSInteger)flag{
//设置frame
CGFloat space = flag*KWidth;
CGFloat X =
self.scroll.contentOffset.x+space;
imageView.frame =
CGRectMake(X, 0,
KWidth, KHeight);
//设置图片
NSInteger imageIndex = X/KWidth;
imageView.image = [UIImage
imageNamed:self.imageNameArray[imageIndex]];
}
#pragma mark - 获得未被利用的imageView
-(void)getUnusedView{
for (int i=0; i<self.imageViewArray.count; i++)
{
UIImageView *imageV =
self.imageViewArray[i];
if (imageV.frame.origin.x!=self.scroll.contentOffset.x)
{
[self.imageUnusedViewArray
addObject:imageV];
}
}
}
@end
#define KWidth self.view.frame.size.width
#define KHeight self.view.frame.size.height
@interface
ViewController ()<UIScrollViewDelegate>
@property(nonatomic,weak)
UIScrollView *scroll;
/**存放图片名的数组*/
@property(nonatomic,strong)
NSMutableArray *imageNameArray;
/**存放3个UIimageView的数组*/
@property(nonatomic,strong)
NSMutableArray *imageViewArray;
/**存放3个UIimageView中未被利用的UIimageView的数组*/
@property(nonatomic,strong)
NSMutableArray *imageUnusedViewArray;
@end
@implementation ViewController
//懒加载(用到时才加载)
-(NSMutableArray *)imageNameArray{
if (_imageNameArray==nil)
{
_imageNameArray = [NSMutableArray
array];
}
return
_imageNameArray;
}
-(NSMutableArray *)imageViewArray{
if (_imageViewArray==nil)
{
_imageViewArray = [NSMutableArray
array];
}
return
_imageViewArray;
}
-(NSMutableArray *)imageUnusedViewArray{
if (_imageUnusedViewArray==nil)
{
_imageUnusedViewArray = [NSMutableArray
array];
}
return
_imageUnusedViewArray;
}
- (void)viewDidLoad {
[super
viewDidLoad];
//加载数据
[self
_loadData];
//加载视图
[self
_loadView];
}
#pragma mark - 加载数据
-(void)_loadData{
for (int i=1; i<12; i++)
{
NSString *imageName = [NSString
stringWithFormat:@"t%i.jpg",i];
[self.imageNameArray
addObject:imageName];
}
}
#pragma mark - 加载视图
-(void)_loadView{
UIScrollView *scroll = [[UIScrollView
alloc]
initWithFrame:self.view.bounds];
[self.view
addSubview:scroll];
scroll.contentSize =
CGSizeMake(KWidth*self.imageNameArray.count,
0);
scroll.pagingEnabled =
YES;
scroll.delegate =
self;
self.scroll = scroll;
//添加三个视图
for (int i=0; i<3; i++)
{
UIImageView *imageView = [[UIImageView
alloc] initWithFrame:CGRectMake(i*KWidth,
0, KWidth,
KHeight)];
imageView.image = [UIImage
imageNamed:self.imageNameArray[i]];
[self.scroll
addSubview:imageView];
[self.imageViewArray
addObject:imageView];
}
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//只有contentOffset是整数时才进行下面判断,这样做就相当于-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView方法
if (scrollView.contentOffset.x/KWidth==(int)(scrollView.contentOffset.x/KWidth))
{
//获取未利用的imageView
[self
getUnusedView];
//不是在第一页,也不是在最后一页的时候
if (scrollView.contentOffset.x>0&&scrollView.contentOffset.x<KWidth*(self.imageNameArray.count-1))
{
[self
setImageFrameWithImage:self.imageUnusedViewArray[0]
andImageWith:frontView];
[self
setImageFrameWithImage:self.imageUnusedViewArray[1]
andImageWith:nextView];
//如果当没利用的imageView数量大于3时,证明当前页面也没有利用,所以当前页面就没有图片(当滑的太快时就会出现以上情况)
if (self.imageUnusedViewArray.count>2)
{
[self
setImageFrameWithImage:self.imageUnusedViewArray[2]
andImageWith:defineView];
}
}
//如果实在第一页
else if(scrollView.contentOffset.x==0)
{
[self
setImageFrameWithImage:self.imageViewArray[0]
andImageWith:defineView];
[self
setImageFrameWithImage:self.imageViewArray[1]
andImageWith:nextView];
}
//如果是在最后一页
else
{
[self
setImageFrameWithImage:self.imageViewArray[0]
andImageWith:defineView];
[self
setImageFrameWithImage:self.imageViewArray[1]
andImageWith:frontView];
}
}
//如果contentOffset不是整数就返回
else
{
return;
}
}
#pragma mark - 设置frame和图片
-(void)setImageFrameWithImage:(UIImageView *)imageView andImageWith:(NSInteger)flag{
//设置frame
CGFloat space = flag*KWidth;
CGFloat X =
self.scroll.contentOffset.x+space;
imageView.frame =
CGRectMake(X, 0,
KWidth, KHeight);
//设置图片
NSInteger imageIndex = X/KWidth;
imageView.image = [UIImage
imageNamed:self.imageNameArray[imageIndex]];
}
#pragma mark - 获得未被利用的imageView
-(void)getUnusedView{
for (int i=0; i<self.imageViewArray.count; i++)
{
UIImageView *imageV =
self.imageViewArray[i];
if (imageV.frame.origin.x!=self.scroll.contentOffset.x)
{
[self.imageUnusedViewArray
addObject:imageV];
}
}
}
@end
相关文章推荐
- expdp query及parfile的使用
- MySql提示:The server quit without updating PID file(…)失败
- iOS学习(6)——UIAlertView
- 贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence
- Java中的break和continue关键字使用总结
- ExtJs中Ext.Ajax.request()跟getForm().submit()得用法
- iOS5中UIViewController的新方法 addChildViewController
- IOS可以拖动的UIButton
- iOS开发UI基础—32直接使用UITableView Controller
- iOS开发UI高级—01常见的项目文件介绍
- Flex builder经常会抛出:进程已终止,没有建立到调试器的连接。error while loading initial content的解决方法
- iOS开发UI基础—30在UItableview中实现加载更多功能
- iOS学习(5)—— UIScrollView
- iOS开发UI基础—29UIScrollView控件实现图片轮播
- iOS开发UI基础—28使用UItableview完成一个简单的QQ好友列表(二)
- Getting Started · Building a RESTful Web Service
- iOS开发UI基础—26简单介绍静态单元格的使用
- iOS开发UI基础—27使用UItableview完成一个简单的QQ好友列表(一)
- iOS开发UI基础—25UITableview控件使用小结
- iOS开发UI基础—24使用纯代码自定义UItableviewcell实现一个简单的微博界面布局