使用UIScrollView缩放图片并保持图片在屏幕中心的正确方式(+双击图片以点击坐标为中心缩放):

//变量定义
@property(strong,nonatomic)UIImageView *imgView;
@property(strong,nonatomic)UIImage *image;
@property(strong,nonatomic)UIScrollView *bgView ;
float maxScale=2.0f;
BOOL isTwiceTaping;

//-----------------设置UIScrollView-----------------------
//初始化
_bgView = [[UIScrollView alloc] init];
_bgView.frame = [UIScreen mainScreen].bounds;
_bgView.backgroundColor = [UIColor blackColor];
_imgView = [[UIImageView alloc] initWithImage:_image];
[_imgView setContentMode:UIViewContentModeScaleAspectFit];
[_bgView addSubview:_imgView];

// 设置_imgView的位置大小
CGRect frame ;
frame.size.width = bgView.frame.size.width;
frame.size.height =frame.size.width*(_image.size.height/_image.size.width);
_imgView.frame = frame;
_imgView.center=bgView.center;
[[[UIApplication sharedApplication] keyWindow] addSubview:bgView];

// 设置WindowLevel与状态栏平级,起到隐藏状态栏的效果
[[[UIApplication sharedApplication] keyWindow] setWindowLevel:UIWindowLevelStatusBar];

//设置最大放大比例、内容大小和代理
_bgView.maximumZoomScale = maxScale;
_bgView.contentSize=_imgView.frame.size;
_bgView.delegate = self;

//隐藏滚动条
_bgView.showsVerticalScrollIndicator = NO;
_bgView.showsHorizontalScrollIndicator = NO;

//添加双击手势
 UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwiceAction:)];
    tapTwice .numberOfTapsRequired = 2;
    tapTwice .numberOfTouchesRequired = 1;
    [_bgView addGestureRecognizer:tapTwice];
//----------------------双击手势操作----------------------
-(void)tapTwiceAction:(UIGestureRecognizer *)sender{
    if(isTwiceTaping){//三次点击不执行
        return;
    }
    isTwiceTaping = YES;
    CGFloat newScale;
    if(_bgView.zoomScale>1){
        newScale=1.0;
    }else{
        newScale=maxScale;
    }
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[sender locationInView:sender.view]];
    [_bgView zoomToRect:zoomRect animated:YES];
    //延时做标记判断,使用户点击3次时的单击效果不生效。
    [self performSelector:@selector(tapTimes) withObject:nil afterDelay:0.35];
    
}
-(void)tapTimes{
    isTwiceTaping = NO;
}
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center
{
    CGRect zoomRect;
    zoomRect.size.height =_bgView.frame.size.height / scale;
    zoomRect.size.width  =_bgView.frame.size.width  / scale;
    zoomRect.origin.x = center.x - (zoomRect.size.width  /2);
    zoomRect.origin.y = center.y - (zoomRect.size.height /2);
    return zoomRect;
}

//------------------------ScrollView代理方法----------------------

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{
return _imgView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
CGFloat xcenter = scrollView.center.x , ycenter = scrollView.center.y;
//目前contentsize的width是否大于原scrollview的contentsize,如果大于,设置imageview中心x点为contentsize的一半,以固定imageview在该contentsize中心。如果不大于说明图像的宽还没有超出屏幕范围,可继续让中心x点为屏幕中点,此种情况确保图像在屏幕中心。

xcenter = scrollView.contentSize.width > scrollView.frame.size.width ? scrollView.contentSize.width/2 : xcenter;

ycenter = scrollView.contentSize.height > scrollView.frame.size.height ? scrollView.contentSize.height/2 : ycenter;

[_imgView setCenter:CGPointMake(xcenter, ycenter)];
}

参考:http://blog.163.com/it__man/blog/static/1371999042013565147299/

你可能感兴趣的:(使用UIScrollView缩放图片并保持图片在屏幕中心的正确方式(+双击图片以点击坐标为中心缩放):)