您的位置:首页 > 产品设计 > UI/UE

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: