【问题标题】:iOS - Zooming UIImageView on a UIScrollViewiOS - 在 UIScrollView 上缩放 UIImageView
【发布时间】:2012-08-16 13:45:07
【问题描述】:

我正在制作一个图片库,在 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];
}

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

【问题讨论】:

  • 找到解决办法了吗?

标签: iphone ios uiimageview gallery zooming


【解决方案1】:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}

【讨论】:

    【解决方案2】:

    示例代码:

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

    【讨论】:

      猜你喜欢
      • 2018-08-24
      • 2012-02-12
      • 2019-05-23
      • 1970-01-01
      • 2012-11-24
      • 2017-01-06
      • 2017-09-23
      • 1970-01-01
      • 2014-01-15
      相关资源
      最近更新 更多