项目需求:图片浏览器(可以左右切换,需要支持手势缩放的功能)https://github.com/Shannoon/XLPhotoBrowser.git ,网上搜罗了很多框架不合适。感谢魏希的分享http://www.jianshu.com/p/03c81ee888f4 。现在贴出来以后备用
效果图如下:
Github:https://github.com/briceZhao/ZoomingImageBrowser
代码
- AZCollectionViewCell.m中
- (void)configSubViews{
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
scrollView.delegate = self;
scrollView.maximumZoomScale = 3.0;//最大缩放倍数
scrollView.minimumZoomScale = 1.0;//最小缩放倍数
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.backgroundColor = [UIColor blackColor];
[self addSubview:scrollView];
self.scrollView = scrollView;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.userInteractionEnabled = YES;//在UIImageView上加手势识别,打开用户交互
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[imageView addGestureRecognizer:doubleTap];//添加双击手势
[scrollView addSubview:imageView];
self.imageView = imageView;
}
- 属性的setImage方法中重新设置UIImageView的图片和尺寸以及ScrollView的缩放比例
- (void)setImage:(UIImage *)image{
_image = image;
self.imageView.image = image;
CGFloat width = image.size.width;
CGFloat height = image.size.height;
CGFloat maxHeight = self.scrollView.bounds.size.height;
CGFloat maxWidth = self.scrollView.bounds.size.width;
//如果图片尺寸大于view尺寸,按比例缩放
if(width > maxWidth || height > width){
CGFloat ratio = height / width;
CGFloat maxRatio = maxHeight / maxWidth;
if(ratio < maxRatio){
width = maxWidth;
height = width*ratio;
}else{
height = maxHeight;
width = height / ratio;
}
}
self.scrollView.contentSize = CGSizeMake(width, height);
[self.scrollView setZoomScale:1.f];
self.imageView.frame = CGRectMake((maxWidth - width) / 2, (maxHeight - height) / 2, width, height);
[self.scrollView layoutIfNeeded];
}
- 实现UIScrollViewDelegate,真正的图片缩放处理
#pragma mark UIScrollViewDelegate
//指定缩放UIScrolleView时,缩放UIImageView来适配
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.imageView;
}
//缩放后让图片显示到屏幕中间
-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
CGSize originalSize = _scrollView.bounds.size;
CGSize contentSize = _scrollView.contentSize;
CGFloat offsetX = originalSize.width > contentSize.width ? (originalSize.width - contentSize.width) / 2 : 0;
CGFloat offsetY = originalSize.height > contentSize.height ? (originalSize.height - contentSize.height) / 2 : 0;
self.imageView.center = CGPointMake(contentSize.width / 2 + offsetX, contentSize.height / 2 + offsetY);
}
- (void)handleDoubleTap:(UITapGestureRecognizer *)recongnizer
{
UIGestureRecognizerState state = recongnizer.state;
switch (state) {
case UIGestureRecognizerStateBegan:
break;
case UIGestureRecognizerStateChanged:
break;
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateEnded:
{
//以点击点为中心,放大图片
CGPoint touchPoint = [recongnizer locationInView:recongnizer.view];
BOOL zoomOut = self.scrollView.zoomScale == self.scrollView.minimumZoomScale;
CGFloat scale = zoomOut?self.scrollView.maximumZoomScale:self.scrollView.minimumZoomScale;
[UIView animateWithDuration:0.3 animations:^{
self.scrollView.zoomScale = scale;
if(zoomOut){
CGFloat x = touchPoint.x*scale - self.scrollView.bounds.size.width / 2;
CGFloat maxX = self.scrollView.contentSize.width-self.scrollView.bounds.size.width;
CGFloat minX = 0;
x = x > maxX ? maxX : x;
x = x < minX ? minX : x;
CGFloat y = touchPoint.y * scale-self.scrollView.bounds.size.height / 2;
CGFloat maxY = self.scrollView.contentSize.height-self.scrollView.bounds.size.height;
CGFloat minY = 0;
y = y > maxY ? maxY : y;
y = y < minY ? minY : y;
self.scrollView.contentOffset = CGPointMake(x, y);
}
}];
}
break;
default:break;
}
}
以上的代码就能实现图片轮播或者图片浏览器/照片查看器等等的浏览图片类,手势缩放的效果了,以上代码参考了魏希的分享http://www.jianshu.com/p/03c81ee888f4 , 感谢原作者。贴出来希望更多的人能用上这个方法