iOS-在UIScrollView上缩放UIImageView [英] iOS - Zooming UIImageView on a UIScrollView

查看:284
本文介绍了iOS-在UIScrollView上缩放UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个图片库,在主UIScrollView的UIScrollViews内有大量的UIImageView. 画廊允许水平更改图片,并对每张图片进行缩放.

I'm making a picture gallery, with an amount of UIImageView inside UIScrollViews, inside a main UIScrollView. The gallery allows to change horizontally the pictures, and doing zoom on each one.

通常情况下效果很好,但是缩放动画根本无法正常工作.

It works fine in general, but the animation of zooming is not working good at all.

当图像较小且未填满整个屏幕时,将出现问题. 当您放大图像,但直到填满整个图片库时,图像才会出现一点位移,我将在下一个代码中对其进行纠正,以使其位于ScrollView的中心.

The problem appears when the image is small and not filling all the screen. When you make bigger the image, but not until filling the full gallery, the image suffers a little displacement, that I correct with the next code, to get it in the center of the 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];
}

最后,图像位于正确的位置,但似乎有点不可思议. 我该怎么办?

At last the image is in the correct place, but it seems a little bit weird. What should I do?

推荐答案

示例代码:

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

这篇关于iOS-在UIScrollView上缩放UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆