UIScrollView 带有负原点的图块

发布于 2024-10-10 02:12:01 字数 420 浏览 5 评论 0原文

我有一个点在屏幕上移动。当它超出滚动视图的边界时,会创建一个新的图块,该点可以移动到该图块上,并且滚动视图的大小会增加。我通过以下方式跟踪这一点:

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

只要新图块具有非负原点,这就可以正常工作。如果新创建的图块位于左边界且原点为负,则边界大小将在右侧进行补偿。我知道 setContentSize 只说明视图有多大,而不说明视图所在的位置。

所以我尝试使用 setContentOffset ,这似乎工作不稳定,但如果我滚动,它会跳回正区域,并且您无法再滚动到负区域。

如何使具有负起源的图块可见?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请远离我 2024-10-17 02:12:01

您可以从内容视图的中间开始,并将 contentSize 设置为最大可能的大小。这样您就不需要动态调整内容大小。

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.

南风起 2024-10-17 02:12:01

从技术上讲,负原点位于滚动视图区域之外。如果您的对象移出该区域,您需要完全重新创建该区域,设置偏移量,使其看起来像您没有重新创建它,然后将内容移动到视图中。或者重新定位内容和点,使其看起来像它们会朝该方向滚动(如果这有意义)...

抽象地说,这就是您定位内容的方式:

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

希望这会有所帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文