如何知道iPhone中当前可见视图的标签

发布于 2024-10-02 23:26:18 字数 121 浏览 0 评论 0原文

我有一个滚动视图,我正在添加一些视图 当用户滚动时如何获取当前可见视图的标签。 然后我可以向该视图添加一些内容...

就像在表视图中获取索引路径一样..

如何做到这一点..?

谢谢

i have an scroll view and i am adding few views to that
when the user scrolls how to get the current visible view's tag.
then i can add some thing to that view...

its just like getting the indexpathrow in table view..

how to do that..?

Thanks

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

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

发布评论

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

评论(1

小巷里的女流氓 2024-10-09 23:26:18

您基本上想要检查 UIScrollView 内的子视图的框架是否与滚动视图的框架相交(如果您只想确定部分可见性),或者该框架是否包含在另一个框架中(如果您想以确定完全可见性)。

但是,为了检查子视图的框架是否相交和/或包含在滚动视图的框架中,您需要将其从滚动视图内的本地坐标转换为滚动视图外的全局坐标。

这可能非常令人困惑,所以这里有一些代码。这将循环遍历滚动视图的所有子视图,并打印出它是完全可见还是部分可见:

for (UIView *subview in scrollView)
{
    CGRect globalRect = CGRectOffset(subview.frame, -scrollView.contentOffset.x, -scrollView.contentOffset.y);
    CGRect scrollViewBounds = CGRectMake(0.0f, 0.0f, scrollView.bounds.size.width, scrollView.bounds.size.height);

    if (CGRectContainsRect(scrollViewBounds, globalRect)) {
        NSLog(@"FULLY VISIBLE");
    } else if (CGRectIntersectsRect(scrollViewBounds, globalRect)) {
        NSLog(@"PARTIALLY VISIBLE");
    }       
}

您可以将其放入 UIScrollViewDelegate 方法中,以便在用户滚动内容时执行这些检查。

You basically want to check if the frame of the subviews inside the UIScrollView intersect the scrollview's frame (if you only want to determine partial visibility), or if the frame is contained in the other frame (if you want to determine full visibility).

However, in order to check if the subview's frame intersects and/or is contained in the scrollview's frame you need to translate it from the local coords inside the scrollview to the global coordinates outside the scrollview.

That is probably pretty confusing, so here is some code. This will loop through all the subviews of a scrollview and print out whether it is fully visibile or partially visible:

for (UIView *subview in scrollView)
{
    CGRect globalRect = CGRectOffset(subview.frame, -scrollView.contentOffset.x, -scrollView.contentOffset.y);
    CGRect scrollViewBounds = CGRectMake(0.0f, 0.0f, scrollView.bounds.size.width, scrollView.bounds.size.height);

    if (CGRectContainsRect(scrollViewBounds, globalRect)) {
        NSLog(@"FULLY VISIBLE");
    } else if (CGRectIntersectsRect(scrollViewBounds, globalRect)) {
        NSLog(@"PARTIALLY VISIBLE");
    }       
}

You could put this in a UIScrollViewDelegate method to do these checks while the user is scrolling the content around.

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