将此代码放入 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)];
添加 Scrollview 和 Gesture 的委托方法
#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;
}