UIScrollView内容不缩放
问题描述:
我有UIImageView里面的UIScrollview,当我尝试捏和缩放,图像跳下来,右(我认为坐标0,0而不是居中),但保持相同的大小,并在同一个地方,直到我停止捏,然后它回到它以前居中的自己。UIScrollView内容不缩放
我得到NSLog在缩放时打印出zoomScale,并且只是保持打印0.5,直到我放开,然后打印1.0一次。
我真的很茫然,所有关于这个的教程看起来非常简单和容易,我不知道我要去哪里错了。
备注: scrollView和ImageView都在storyboard中,连接到插座。它们所在的viewController是一个实现了viewForZoomingInScrollView:的UIScrollView委托。 minimumScale是1.0,maximumScale是4.0,尽管这些数字似乎没有任何影响。
答
将这个代码在viewDidLoad中
yourScroll.bouncesZoom = YES;
yourScroll.delegate = self;
yourScroll.clipsToBounds = YES;
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];
[twoFingerTap setNumberOfTouchesRequired:2];
[yourImageView addGestureRecognizer:twoFingerTap];
float minimumScale = 1.0;//This is the minimum scale, set it to whatever you want. 1.0 = default
yourScroll.maximumZoomScale = 4.0;
yourScroll.minimumZoomScale = minimumScale;
yourScroll.zoomScale = minimumScale;
[yourScroll setContentMode:UIViewContentModeScaleAspectFit];
[yourScroll sizeToFit];
[yourScroll setContentSize:CGSizeMake(yourImageView.frame.size.width, yourImageView.frame.size.height)];
添加滚动视图的委托方法和手势
#pragma mark UIScrollViewDelegate methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return yourImageView;
}
#pragma mark TapDetectingImageViewDelegate methods
- (void)scrollViewDidZoom:(UIScrollView *)aScrollView {
CGFloat offsetX = (yourScroll.bounds.size.width > yourScroll.contentSize.width)?
(yourScroll.bounds.size.width - yourScroll.contentSize.width) * 0.5 : 0.0;
CGFloat offsetY = (yourScroll.bounds.size.height > yourScroll.contentSize.height)?
(yourScroll.bounds.size.height - yourScroll.contentSize.height) * 0.5 : 0.0;
yourImageView.center = CGPointMake(yourScroll.contentSize.width * 0.5 + offsetX,
yourScroll.contentSize.height * 0.5 + offsetY);
}
- (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer {
// two-finger tap zooms out
float newScale = [previewScroll zoomScale]/ZOOM_STEP;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
[yourScroll 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 = [previewScroll frame].size.height/scale;
zoomRect.size.width = [previewScroll 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;
}
+2
这对编程有帮助,但包括Apple自己的文档在内的每个教程都说我唯一需要做代码的事情是实现viewForZoomingInScrollView – Mirror318 2013-04-07 21:10:47
https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ UIScrollView_pg/ZoomZoom/ZoomZoom.html – Alex 2013-04-07 09:58:08
感谢iOS10,该文档看起来非常简单直接,但它怎么不适合我?我设置委托,并实现viewForZoomingInScrollView :,还有什么可以做的? – Mirror318 2013-04-07 21:35:23