如何确定滚动视图中当前可见的区域并确定中心?

时间:2012-02-29 17:31:12

标签: iphone ios uiscrollview

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

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分: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 );