开发者

UIScrollView with tiles with negative origin

I have a dot that's moving around the 开发者_开发知识库screen. As it exceeds the bound of the scroll view, a new tile is create that the dot can move onto and the size of the scroll view is increased. I keep track of this with:

scrollViewBounds = CGRectUnion(newTileBounds, scrollViewBounds);
[scrollView setContentSize:scrollViewBounds.size];

This works fine as long as the new tiles have a non-negative origin. If the newly created tiles are to the left border and have a negative origin, the bounds size will be compensated on the right side. I know that setContentSize only say how big the view will be, not where it is located.

So I tried using setContentOffset and that seemed to work choppily, but then if I scrolled, it would jump back to the positive area and you couldn't scroll to the negative region anymore.

How do you make the tiles with negative origins be visible?


You could start off in the middle of the content view and contentSize set to the maximum possible size. This way you don't need to dynamically adjust the content size.


Technically, negative origins are outside of the scroll view area. If your object moves outside of that area, you need to recreate the area entirely, set the offset so that it looks like you haven't recreated it, then move your content into view. Or reposition both the content and the dot so that it looks like they'll be scrolling in that direction (if that makes sense)...

Abstractly, this is how you'd position the content:

if(object.origin.x < 0) {
 contentArea.origin.x = -contentArea.bounds.width;
 object.origin.x = contentArea.bounds.width + object.origin.x;
}

Hopefully that helps!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜