//自定义cell类,继承于UICollectionViewCell
@interface MyCell :UICollectionViewCell
@property (nonatomic,retain) UIScrollView *scrollView;
@property (nonatomic,retain) UIImageView *showImgView;
@end
@implementation MyCell
- (id)initWithFrame:(CGRect)frame {
self= [super initWithFrame:frame];
if(self) {
[self addSubview:self.scrollView];
}
return self;
}
- (UIImageView *)showImgView {
if(!_showImgView) {
_showImgView = [[UIImageView alloc] init];
_showImgView.frame = self.bounds;
}
return _showImgView;
}
- (UIScrollView *)scrollView {
if(!_scrollView) {
_scrollView= [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.delegate=self;
//设置图片缩放比例
_scrollView.minimumZoomScale= 0.5f;
_scrollView.maximumZoomScale= 2.0f;
[_scrollView addSubview:self.showImgView];
}
return _scrollView;
}
#pragma mark --- UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _showImgView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
CGFloat offsetX = scrollView.bounds.size.width> scrollView.contentSize.width? (scrollView.bounds.size.width- scrollView.contentSize.width) * 0.5 : 0.0;
CGFloat offsetY = scrollView.bounds.size.height> scrollView.contentSize.height? (scrollView.bounds.size.height- scrollView.contentSize.height) * 0.5 : 0.0;
_showImgView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX, scrollView.contentSize.height/2 + offsetY);
}
#import"MyCell.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interfaceViewController()
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *imageArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建图片
[self createImageArray];
//设置collectionView
[self.view addSubview:self.collectionView];
}
- (void)createImageArray {
_imageArray= [NSMutableArray array];
for(int i = 0; i<9; i++) {
UIImage *image = [UIImage imageNamed:[NSStringstringWithFormat:@"%d.jpg",i+1]];
[_imageArray addObject:image];
}
}
- (UICollectionView *)collectionView {
if(!_collectionView) {
//布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 0;
layout.minimumInteritemSpacing = 0;
[layout setItemSize:CGSizeMake(WIDTH,HEIGHT)];
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_collectionView= [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.pagingEnabled = YES;
//_collectionView.minimumZoomScale = 0.5f;
//_collectionView.maximumZoomScale = 2.0f;
[_collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"cell"];
}
return _collectionView;
}
//- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
// int index = _collectionView.contentOffset.x / WIDTH;
// MyCell *cell = self.collectionView.subviews[index];
// return cell.showImgView;
//}
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section {
return _imageArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//在cell上显示图片
cell.showImgView.image= [_imageArray objectAtIndex:indexPath.row];
//对cell上的图片大小进行操作
[self resetImageWithImgView:cell.showImgView];
//每次加载视图时让滑动视图的缩放系数为1
cell.scrollView.zoomScale= 1.0f;
returncell;
}
- (void)resetImageWithImgView:(UIImageView *)imgView {
//获取图片的原始宽高
CGSize size = imgView.image.size;
//获取缩放比例
CGFloat xScale = WIDTH/ size.width;
CGFloat yScale = HEIGHT/ size.height;
//以小的为标准
if(xScale < yScale) {
CGRect rect = imgView.frame;
rect.size.width=WIDTH;
rect.size.height = size.height * xScale;
imgView.frame= rect;
}else{
CGRect rect = imgView.frame;
rect.size.height=HEIGHT;
rect.size.width = size.width* yScale;
imgView.frame = rect;
}
//居中
imgView.center = self.view.center;
}
网友评论