思路:
一般多张图片的展示都是放在UIScrollView展示。那么图片的放大所有功能一般也是依赖UIScrollView的放大功能。但是多张图片展示,要放大一张图片,会导致UIScrollView的位置变化而恢复不到原来的位置,滑动会造成闪退。那么我们换一个角度,自定义一个图片展示器,这个图片展示器自带放大缩小功能。那么就解决上述问题了。思路已有,那么实现就不是问题了。下面直接上代码。
功能:
图片具有,放大,缩小,单击,双击等功能。
实现:
创建WMImageview类。继承与UIView 该类为图片展示类。
用于显示图片的imageView
@property(nonatomic,strong)UIImageView *imageView;
用于图片缩放的滚动视图
@property(nonatomic,strong)UIScrollView *scrollView;
重新计算位置,把图片以合理的位置展示出来
-(void)calculateImageSizeToShow:(UIImage *)image
初始化试图 传入frame
-(id)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
//初始化试图
[self setupView];
}
return self;
}
初始化试图和注册手势,主要是把UIImageView放在UIScrollView上面,然后把UIScrollView的坐标赋给UIImageView。这样利用UIScrollView的放缩性质可以实现图片的放大效果。
- (void)setupView {
//下方的滚动视图用于图片的缩放
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
[_scrollView setContentSize:self.bounds.size];
_scrollView.pagingEnabled = NO;
_scrollView.minimumZoomScale = 1;
_scrollView.maximumZoomScale = 3;
_scrollView.delegate = self;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator=NO;
_scrollView.backgroundColor = [UIColor lightBackColor];
[self addSubview:_scrollView];
//显示的imageView
_imageView = [[UIImageView alloc] initWithFrame:_scrollView.frame];
_imageView.contentMode = UIViewContentModeScaleAspectFill;
_imageView.clipsToBounds = YES;
_imageView.userInteractionEnabled = YES;
_imageView.backgroundColor = [UIColor blackColor];
[_scrollView addSubview:_imageView];
/** 以下是用手势来写点击事件
* singleTap 单指一次点击事件
* doubleTap 单指两次点击事件 */
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
singleTap.delaysTouchesBegan = YES;
singleTap.numberOfTapsRequired = 1;
[_imageView addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
doubleTap.numberOfTapsRequired = 2;
[_imageView addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
}
图片重新计算位置,然后显示新图片位置的实现
-(void)calculateImageSizeToShow:(UIImage *)image{
//宽比例
CGFloat widthRotio = CGRectGetWidth(self.frame)/(image.size.width*1.0f);
//高比例
CGFloat heightRotio = CGRectGetHeight(self.frame)/(image.size.height*1.0f);
//为了满足双方,选择最小的比例为我们要展示的比例
CGFloat wantRotio = MIN(widthRotio, heightRotio);
//按理想比例去计算新的宽和高
CGFloat viewWidth = wantRotio * image.size.width;
CGFloat viewHeight = wantRotio * image.size.height;
CGRect newRect = CGRectMake((CGRectGetWidth(self.frame)-viewWidth)/2, (CGRectGetHeight(self.frame)-viewHeight)/2, viewWidth, viewHeight);
[_imageView setFrame:newRect];
}
注册单击手势的处理——在非放大状态下,退出展示视图
- (void)handleSingleTap:(UITapGestureRecognizer *)tap {
if (!_isDoubleTap)
{ //如果不是放大状态,那么单击就是推出
if (self.singTapHide)
{
self.singTapHide();
}
}
}
注册双击手势的处理——以点击区域为中心进行放大。首先判断视图是否在原来状态。不在放大状态就进行最大放大倍数和最小放大倍数之和的0.5倍放大。
- (void)handleDoubleTap:(UITapGestureRecognizer *)tap {
//获取点击的中心位置
CGPoint touchPoint = [tap locationInView:_imageView];
if (_scrollView.zoomScale != _scrollView.minimumZoomScale && _scrollView.zoomScale != [self initialZoomScaleWithMinScale]) {
// Zoom out
[_scrollView setZoomScale:_scrollView.minimumZoomScale animated:YES];
_isDoubleTap = NO;
} else {
CGFloat newZoomScale = ((_scrollView.maximumZoomScale + _scrollView.minimumZoomScale) / 2);
CGFloat xsize = self.bounds.size.width / newZoomScale;
CGFloat ysize = self.bounds.size.height / newZoomScale;
[_scrollView zoomToRect:CGRectMake(touchPoint.x - xsize/2, touchPoint.y - ysize/2, xsize, ysize) animated:YES];
_isDoubleTap = YES;
}
- (CGFloat)initialZoomScaleWithMinScale {
CGFloat zoomScale = _scrollView.minimumZoomScale;
if (_imageView) {
// Zoom image to fill if the aspect ratios are fairly similar
CGSize boundsSize = self.bounds.size;
CGSize imageSize = _imageView.image.size;
CGFloat boundsAR = boundsSize.width / boundsSize.height;
CGFloat imageAR = imageSize.width / imageSize.height;
CGFloat xScale = boundsSize.width / imageSize.width; // the scale needed to perfectly fit the image width-wise
CGFloat yScale = boundsSize.height / imageSize.height; // the scale needed to perfectly fit the image height-wise
// Zooms standard portrait images on a 3.5in screen but not on a 4in screen.
if (ABS(boundsAR - imageAR) < 0.17) {
zoomScale = MAX(xScale, yScale);
// Ensure we don't zoom in or out too far, just in case
zoomScale = MIN(MAX(_scrollView.minimumZoomScale, zoomScale), _scrollView.maximumZoomScale);
}
}
return zoomScale;
}
UIScrollViewDelegate的代理的实现
//返回给他一个可以缩放的视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _imageView;
}
//返回返回视图的样式
- (void)setScaleImageToNormal {
[_scrollView setZoomScale:1 animated:YES];
}
正在缩放的时候会调用 保证缩放始终是一中心位置
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
//这里要设置缩放的imageView 始终居中
CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;
CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;
_imageView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);
}
一个阶段的缩放停止的时候会调用
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
if (scale < 1)
{
[scrollView setZoomScale:scrollView.minimumZoomScale animated:YES];
_isDoubleTap = NO;
}
}