使用uicollectionview+scrollview的方式實(shí)現(xiàn)一個(gè)簡(jiǎn)易相冊(cè)瀏覽功能
代碼參見(jiàn)* https://github.com/gyf1993/owenImageBrowser.git
cell初始化方法
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
self.scrollView.minimumZoomScale = MINZOOMSCALE;
self.scrollView.maximumZoomScale = MAXZOOMSCALE;
self.scrollView.delegate = self;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
[self.scrollView addGestureRecognizer:tap];
[self.contentView addSubview:self.scrollView];
self.imageView = [[UIImageView alloc]initWithFrame:frame];
self.imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleClickAction:)];
doubleTap.numberOfTouchesRequired = 1;
doubleTap.numberOfTapsRequired = 2;
[self.imageView addGestureRecognizer:doubleTap];
[tap requireGestureRecognizerToFail:doubleTap];
[self.scrollView addSubview:self.imageView];
}
return self;
}
scrollview代理方法
返回scrollview中進(jìn)行縮放的view
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
縮放監(jiān)聽(tīng)代理鲤桥,更新imageview的center值
- (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;
scrollView.subviews[0].center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
scrollView.contentSize.height * 0.5 + offsetY);
}
cell復(fù)用調(diào)用設(shè)置image方法
- (void)setImage:(NSString *)imageName
{
//重置zoomscale為1
self.scrollView.zoomScale = 1;
UIImage *image = [UIImage imageNamed:imageName];
self.imageView.image = image;
CGFloat x = SCREEN_HEIGHT/SCREEN_WIDTH;
CGFloat y = image.size.height/image.size.width;
//x為屏幕尺寸,y為圖片尺寸舌界,通過(guò)兩個(gè)尺寸的比較,重置imageview的frame
if (y>x) {
self.imageView.frame = CGRectMake(0, 0, SCREEN_HEIGHT/y, SCREEN_HEIGHT);
}else
{
self.imageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH*y);
}
self.imageView.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
}
雙擊放大圖片方法
雙擊放大需要我們通過(guò)設(shè)置scrollview的zoomscale以及contentoffset來(lái)實(shí)現(xiàn),處理起來(lái)稍微復(fù)雜一點(diǎn)既棺,contentoffset的設(shè)置需要根據(jù)雙擊坐標(biāo)在圖片中的位置分為4種情況(左上湾笛、左下楣导、右上俯树、右下)進(jìn)行處理帘腹。
- (void)doubleClickAction:(UIGestureRecognizer *)gesture
{
CGFloat k = MAXZOOMSCALE;
UIScrollView *scrollView = (UIScrollView *)gesture.view.superview;
CGFloat width = gesture.view.frame.size.width;
CGFloat height = gesture.view.frame.size.height;
CGPoint point = [gesture locationInView:gesture.view];
//獲取雙擊坐標(biāo),分4種情況計(jì)算scrollview的contentoffset
if (point.x<=width/2 && point.y<=height/2) {
point = CGPointMake(point.x*k, point.y*k);
point = CGPointMake(point.x-SCREEN_WIDTH/2>0?point.x-SCREEN_WIDTH/2:0,point.y-SCREEN_HEIGHT/2>0?point.y-SCREEN_HEIGHT/2:0);
}else if (point.x<=width/2 && point.y>height/2)
{
point = CGPointMake(point.x*k, (height-point.y)*k);
point = CGPointMake(point.x-SCREEN_WIDTH/2>0?point.x-SCREEN_WIDTH/2:0,point.y>SCREEN_HEIGHT/2?height*k-point.y-SCREEN_HEIGHT/2:height*k>SCREEN_HEIGHT?height*k-SCREEN_HEIGHT:0);
}else if (point.x>width/2 && point.y<=height/2)
{
point = CGPointMake((width-point.x)*k, point.y*k);
point = CGPointMake(point.x>SCREEN_WIDTH/2?width*k-point.x-SCREEN_WIDTH/2:width*k>SCREEN_WIDTH?width*k-SCREEN_WIDTH:0, point.y-SCREEN_HEIGHT/2>0?point.y-SCREEN_HEIGHT/2:0);
}else
{
point = CGPointMake((width-point.x)*k, (height-point.y)*k);
point = CGPointMake(point.x>SCREEN_WIDTH/2?width*k-point.x-SCREEN_WIDTH/2:width*k>SCREEN_WIDTH?width*k-SCREEN_WIDTH:0, point.y>SCREEN_HEIGHT/2?height*k-point.y-SCREEN_HEIGHT/2:height*k>SCREEN_HEIGHT?height*k-SCREEN_HEIGHT:0);
}
if (scrollView.zoomScale == 1) {
[UIView animateWithDuration:0.3 animations:^{
scrollView.zoomScale = k;
scrollView.contentOffset = point;
} completion:^(BOOL finished) {
}];
}else
{
[UIView animateWithDuration:0.3 animations:^{
scrollView.zoomScale = 1;
}];
}
}