iOS – 在UIScrollView上缩放UIImageView
我在一个主UIScrollView内制作了一个图片库,在UIScrollViews里面有一定数量的UIImageView。 画廊允许水平改变图片,并在每一个变焦。
一般来说,它工作得很好,但是缩放的animation根本不起作用。
问题出现在图像很小而没有填满整个屏幕时。 当你做出更大的图像,但直到填充完整的图库,图像遭受一点点位移,我纠正与下一个代码,让它在ScrollView的中心。
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; aImg.frame = [self centeredFrameForScrollView:myScrollView andUIView:aImg]; [UIView commitAnimations]; }
最后形象是在正确的地方,但似乎有点奇怪。 我该怎么办?
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return imageView; }
示例代码:
- (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer { // two-finger tap zooms out float newScale = [scrlView zoomScale] / ZOOM_STEP; CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]]; [scrlView zoomToRect:zoomRect animated:YES]; } - (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 = [scrlView frame].size.height / scale; zoomRect.size.width = [scrlView frame].size.width / scale; // choose an origin so as to get the right center. zoomRect.origin.x = scrlView.center.x - (zoomRect.size.width / 2.0); zoomRect.origin.y = scrlView.center.y - (zoomRect.size.height / 2.0); return zoomRect; }