1

我正在制作一个图片库,在 UIScrollViews 内有大量 UIImageView,在主 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];
}

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

4

2 回答 2

1
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
于 2012-08-21T10:08:33.633 回答
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;
}
于 2013-01-02T14:11:42.537 回答