开发者

How to know the present visible view's tag in iphone

开发者 https://www.devze.com 2023-01-25 07:43 出处:网络
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.

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 th开发者_Go百科ing to that view...

its just like getting the indexpathrow in table view..

how to do that..?

Thanks


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.

0

精彩评论

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

关注公众号