开发者

Determining the center of the viewed space in a UIScrollView

I am displaying a CATiledLayer via a scrollview which displays a map. When the user presses a button, I want to determine the bounds of the catiledlayer that the user is currently viewing and place an image there.

In other words, I need to determine the portion of the CAT开发者_如何转开发iledLayer that is being displayed, then determine the center point of that portion.

Appreciate any thoughts.


You can call scrollView.contentOffset (which returns a CGPoint) which will give you where all the content in your scroll view is in relation to the origin point of the scroll view. So if your CATiledLayer is up and to the left you'd get some thing like (-50, -100). Take those values, multiply by -1 and then add them to scrollView.center property (also returns a CGPoint). This will give you a CGPoint coordinate that if used as the center point of a new-subview within scrollView will center that view within the current frame.

CGPoint currentOffset = CGPointMake(scrollView.contentOffset.x * -1, scrollView.contentOffset.y * -1);
CGPoint center = CGRectMake(scrollView.center.x + currentOffset.x, scrollView.center.y + currentOffset.y);

I haven't tested this code, but it should work or at least give you a general starting point!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜