如何在uiscrollview中双击缩放和两个手指缩放uiimage
Posted
技术标签:
【中文标题】如何在uiscrollview中双击缩放和两个手指缩放uiimage【英文标题】:how to double tap zoom and two fingure zoom uiimage in uiscrollview 【发布时间】:2013-06-12 09:43:28 【问题描述】:嗨,我实际上在 uiscrollview 中有多个图像。实际上我想像在 iphone 图片库中那样实现图像滚动。我正在将图像从 url 下载到 imagearray,然后我可以成功地在 uiscrollview 中从 imagearray 显示和滚动这些图像。但我的问题是我想在 uiscrollviiew 中启用双击放大/缩小和两个手指放大/缩小,就像在 iphone 画廊中一样。就像用户双击图像会放大一样。当用户再次双击该图像时,该图像将缩放。以及双手指手势的相同情况。我试图实现它阅读教程但实际上不能成功问题是当我双击一个图像双击图像方法成功但可以对图像大小产生影响这是我为滚动和手势识别实现的代码 对于手势识别,我使用了来自苹果网站的代码一
if (imagesArray.count < 1)
msgView = [[UIView alloc]initWithFrame:CGRectMake(35, 190, 250, 40)];
[msgView setBackgroundColor:[UIColor blackColor]];
[msgView setAlpha:0.5];
UILabel *mgtLbl = [[UILabel alloc]initWithFrame:CGRectMake(25, 8, 250, 25)];
[mgtLbl setText:@"Sorry no image available for this post"];
[mgtLbl setFont:[UIFont systemFontOfSize:13.0]];
[mgtLbl setBackgroundColor:[UIColor clearColor]];
[mgtLbl setTextColor:[UIColor blueColor]];
[msgView addSubview:mgtLbl];
[self.view addSubview:msgView];
else
[msgView removeFromSuperview];
[self.view setBackgroundColor:[UIColor blackColor]];
srcVw = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
srcVw.scrollEnabled = YES;
srcVw.delegate = self;
[srcVw setZoomScale:1.0];
srcVw.showsHorizontalScrollIndicator = YES;
srcVw.showsVerticalScrollIndicator = NO;
srcVw.pagingEnabled = NO;
//add the scrollview to the view
srcVw = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height)];
srcVw.pagingEnabled = YES;
[srcVw setAlwaysBounceVertical:NO];
//setup internal views
NSInteger numberOfViews = imagesArray.count;
for (int i = 0; i < numberOfViews; i++)
CGFloat xOrigin = i * self.view.frame.size.width;
image = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0,self.view.frame.size.width,400)];
/*
[image setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat: @"http://www.junkremoval.co.uk/bedspace/%@",[imagesArray objectAtIndex:i]]]];
*/
image.image = [imagesArray objectAtIndex:i];
image.contentMode = UIViewContentModeScaleAspectFit;
// image.frame = (CGRect).origin=CGPointMake(0.0f, 0.0f), .size=image.image.size;
[srcVw addSubview:image];
//set the scroll view content size
srcVw.contentSize = CGSizeMake(self.view.frame.size.width *numberOfViews,
self.view.frame.size.height);
[self.view addSubview:srcVw];
[image setTag:ZOOM_VIEW_TAG];
[image setUserInteractionEnabled:YES];
// add gesture recognizers to the image view
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];
[doubleTap setNumberOfTapsRequired:2];
[twoFingerTap setNumberOfTouchesRequired:2];
[image addGestureRecognizer:singleTap];
[image addGestureRecognizer:doubleTap];
[image addGestureRecognizer:twoFingerTap];
// calculate minimum scale to perfectly fit image width, and begin at that scale
float minimumScale = [srcVw frame].size.width / [image frame].size.width;
[srcVw setMinimumZoomScale:minimumScale];
[srcVw setZoomScale:minimumScale];
/************************************** NOTE **************************************/
/* The following delegate method works around a known bug in zoomToRect:animated: */
/* In the next release after 3.0 this workaround will no longer be necessary */
/**********************************************************************************/
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
[scrollView setZoomScale:scale+0.01 animated:NO];
[scrollView setZoomScale:scale animated:NO];
#pragma mark TapDetectingImageViewDelegate methods
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer
// single tap does nothing for now
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer
// double tap zooms in
float newScale = [srcVw zoomScale] * ZOOM_STEP;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
[srcVw zoomToRect:zoomRect animated:YES];
- (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer
// two-finger tap zooms out
float newScale = [srcVw zoomScale] / ZOOM_STEP;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
[srcVw zoomToRect:zoomRect animated:YES];
#pragma mark Utility methods
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center
CGRect zoomRect;
// the zoom rect is in the content view's coordinates.
// At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
// As the zoom scale decreases, so more content is visible, the size of the rect grows.
zoomRect.size.height = [srcVw frame].size.height / scale;
zoomRect.size.width = [srcVw frame].size.width / scale;
// choose an origin so as to get the right center.
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
return zoomRect;
【问题讨论】:
c-sharpcorner.com/UploadFile/d49768/image-zooming-in-iphone ***.com/questions/9008975/… 【参考方案1】:我猜你有你需要的一切here
【讨论】:
请阅读这篇元帖子:meta.stackexchange.com/questions/8231/… 正如其他人所说,您需要添加相关部分,否则这是不可接受的答案。以上是关于如何在uiscrollview中双击缩放和两个手指缩放uiimage的主要内容,如果未能解决你的问题,请参考以下文章
在viewPager中双指缩放图片,双击缩放图片,单指拖拽图片