【问题标题】:How can I determine the area currently visible in a scrollview and determine the center?如何确定滚动视图中当前可见的区域并确定中心?
【发布时间】:2012-02-29 17:31:11
【问题描述】:

我有一个地图应用程序,用户可以在其中手动放置航点。我希望他们按下航点按钮,并在内容视图上当前可见视图的中心放置一个航点。

【问题讨论】:

    标签: iphone ios uiscrollview


    【解决方案1】:

    恐怕您必须自己计算。 contentSize 返回滚动内容的大小,contentOffset 为您提供内容内滚动视图的原点。然后使用scrollView.bounds.size 可以找到视图的中心。

    尚未对此进行测试,但也许您可以像这样将scrollView.center 转换为您的滚动地图:

    CGPoint viewportCenterInMapCoords = 
            [scrollView.superview convertPoint:scrollView.center
                                        toView:mapViewInsideScrollView];
    

    【讨论】:

    • 谢谢 - 您提供的代码不太有效,尽管您的回答确实让我想出了答案。问题在于缩放系数。
    【解决方案2】:

    需要考虑它的缩放程度,然后我可以将内容偏移量转换为完整图像的大小并添加一些。 /// 这是地图图像的全尺寸 CGSize fullSize = CGPointMake(13900, 8400);

        /// determines how the current content size compares to the full size
        float zoomFactor = size.width/self.contentSize.width; 
    
        /// apply the zoom factor to the content offset , this basically upscales 
        /// the content offset to apply to the dimensions of the full size map
    
        float newContentOffsetX = self.contentOffset.x*zoomFactor + (self.bounds.size.width/2) *zoomFactor-300;
        float newContentOffsetY = self.contentOffset.y*zoomFactor + (self.bounds.size.height/2) * zoomFactor-300;
    
        /// not sure why i needed to subtract the 300, but the formula wasn't putting 
        /// the point in the exact center, subtracting 300 put it there in all situations though 
    
        CGPoint point = CGPointMake(newContentOffsetX,newContentOffsetY );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多