iOS - 在UIScrollView上缩放UIImageView

时间:2012-08-21 08:37:26

标签: iphone ios uiimageview gallery zoom

我正在UIScrollViews中创建一个图片库,其中包含一些UISmageView,位于主UIScrollView中。 图库允许水平更改图片,并对每个图片进行缩放。

一般来说效果很好,但缩放动画效果不佳。

当图像很小而没有填满所有屏幕时,会出现问题。 当您使图像变大时,但直到填满整个图库时,图像会受到一点位移,我会使用下一个代码进行校正,以使其位于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];
}

最后图像在正确的位置,但似乎有点奇怪。 我该怎么办?

2 个答案:

答案 0 :(得分:1)

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}

答案 1 :(得分:1)

示例代码:

- (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;
}