【问题标题】:Get the zoomed frame of UIScrollView relative to original size获取 UIScrollView 相对于原始大小的缩放框
【发布时间】:2010-10-04 08:17:28
【问题描述】:

我有一个 UIScrollView 并在其中添加了一个 UIView,当我对其进行缩放时,是否可以获得缩放帧相对于原始帧的 CGRect?

例如我有 800x600 帧,然后我缩放到 {{50, 60}, {100, 100}} 是否可以以编程方式获取缩放帧?

【问题讨论】:

    标签: iphone objective-c uiscrollview


    【解决方案1】:

    我通常使用以下方法(添加到自定义的 UIScrollView 类别中):

    - (CGRect) visibleRect{
        CGRect visibleRect;
    
        visibleRect.origin = self.contentOffset;
        visibleRect.size = self.bounds.size;
    
        visibleRect.origin.x /= self.zoomScale;
        visibleRect.origin.y /= self.zoomScale;
        visibleRect.size.width /= self.zoomScale;
        visibleRect.size.height /= self.zoomScale;
        return visibleRect;
    }
    

    【讨论】:

    • 如果您对我的回答发表评论,我们将不胜感激。
    【解决方案2】:

    @Vladimir 解决方案的问题是,如果 viewForZoom 小于 ScrollView 边界,它会显示 visibleRect 错误。所以我想出了这个解决方案。

    - (CGRect) zoomedFrame{
        CGRect zoomedFrame;
    
        zoomedFrame.origin = self.contentOffset;
        zoomedFrame.origin.x -= zoomingView.frame.origin.x;
        zoomedFrame.origin.y -= zoomingView.frame.origin.y;
        zoomedFrame.size = self.contentSize;
        return zoomedFrame;
    }
    

    zoomingView 是一个返回 viewForZoomingInScrollView: 方法的视图。
    bounds 是 scrollView 的边界。


    所以有两种情况:

    1. zoomingView 小于bounds 时,contentOffset 反映的不是内容视图的左上角,而是内容视图相对于bounds 中心的一些奇怪移动。并且zoomingView.frame.origin 具有正常值,就好像zoomingViewbounds 的中心一样。 (如果您尝试缩小 zoomingView 超过minimulScale,就会发生这种情况)

    2. zoomingViewbounds 大时,zoomingView.frame.origin 具有如下奇怪的值:

      {-6.15367e-06, 3.98168e-06}

      contentOffset 显示了它should 的内容。


    因此,正如我在代码中显示的那样,所有这些都相互补偿。

    【讨论】:

      猜你喜欢
      • 2012-01-24
      • 2017-11-04
      • 2021-07-09
      • 2016-06-09
      • 2020-07-16
      • 2017-03-09
      • 2015-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多