单击双击放大图片

Posted aotuman

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单击双击放大图片相关的知识,希望对你有一定的参考价值。

-(void)create{   

 _scrollerView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, YHScreemW, YHScreemH)];

    

    _scrollerView.showsVerticalScrollIndicator = NO;

    _scrollerView.showsHorizontalScrollIndicator = NO;

    _scrollerView.minimumZoomScale = 1.0;

    _scrollerView.maximumZoomScale = 2.0;

    _scrollerView.delegate = self;

    [self.view addSubview:_scrollerView];

    

    

    //粘贴一张图片

    _imageView = [[UIImageView alloc] init];

    _imageView.frame = CGRectMake(0, 0, _scrollerView.frame.size.width, _scrollerView.frame.size.height);

    _imageView.center = CGPointMake(_scrollerView.frame.size.width/2, _scrollerView.frame.size.height/2);

    _imageView.contentMode = UIViewContentModeScaleAspectFit;

    [_imageView setImage:[UIImage imageWithData:_picData]];

    [_imageView setUserInteractionEnabled:YES];

    [_scrollerView addSubview:_imageView];

    

    

    

    //添加双击事件

    UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];

    [doubleTapGesture setNumberOfTapsRequired:2];

    [_imageView addGestureRecognizer:doubleTapGesture];

    

    

    //添加双击事件

    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGesture)];

    [singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];//没有这个单击会阻挡双击

    [_imageView addGestureRecognizer:singleTapGesture];

}

 

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

{

    return _imageView;

}

- (void)handleDoubleTap:(UIGestureRecognizer *)gesture

{

    CGFloat zoomScale = _scrollerView.zoomScale;

    zoomScale = (zoomScale == 1.0) ? 2.0 : 1.0;

    CGRect zoomRect = [self zoomRectForScale:zoomScale withCenter:[gesture locationInView:gesture.view]];

    [_scrollerView zoomToRect:zoomRect animated:YES];

}

 

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center

{

    CGRect zoomRect;

    zoomRect.size.height =_scrollerView.frame.size.height / scale;

    zoomRect.size.width  =_scrollerView.frame.size.width  / scale;

    zoomRect.origin.x = center.x - (zoomRect.size.width  /2.0);

    zoomRect.origin.y = center.y - (zoomRect.size.height /2.0);

    return zoomRect;

}

 

-(void)singleTapGesture{

    

    [self dismissViewControllerAnimated:YES completion:nil];

    

}

 

以上是关于单击双击放大图片的主要内容,如果未能解决你的问题,请参考以下文章

Excel插入图片实现单击放大或缩小

Excel插入图片实现单击放大或缩小

Excel插入图片实现单击放大或缩小

winform窗体的实现单击按钮实现图片放大缩小

iOS—仿微信单击放大图片

photoswipe 实现图片的单击放大