項目需求:圖片瀏覽器(可以左右切換,需要支持手勢縮放的功能)https://github.com/Shannoon/XLPhotoBrowser.git ,網上搜羅了很多框架不合適。感謝魏希的分享http://www.itdecent.cn/p/03c81ee888f4 ?,F(xiàn)在貼出來以后備用
效果圖如下:

photoBrowser.gif
Github:https://github.com/briceZhao/ZoomingImageBrowser
代碼
- AZCollectionViewCell.m中
- (void)configSubViews{
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
scrollView.delegate = self;
scrollView.maximumZoomScale = 3.0;//最大縮放倍數(shù)
scrollView.minimumZoomScale = 1.0;//最小縮放倍數(shù)
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];
}
- 實現(xiàn)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;
}
}
以上的代碼就能實現(xiàn)圖片輪播或者圖片瀏覽器/照片查看器等等的瀏覽圖片類,手勢縮放的效果了,以上代碼參考了魏希的分享http://www.itdecent.cn/p/03c81ee888f4 , 感謝原作者。貼出來希望更多的人能用上這個方法