11

如果我有一个可见的UIView(或UIView子类),我如何判断它当前是否显示在屏幕上(而不是,例如,在当前不在屏幕的滚动视图的一部分中)?

为了让你更好地理解我的意思,UITableView有几种方法可以确定当前可见单元格的集合。我正在寻找一些可以对任何给定的UIView.

4

4 回答 4

11

还没有尝试过任何这些。但是CGRectIntersectsRect()-[UIView convertRect:to(from)View]并且-[UIScrollView contentOffset]似乎是您在这里的基本构建块。

于 2008-09-28T02:35:55.457 回答
2

这是我用来检查哪些 UIView 在 UIScrollView 中可见的内容:

for(UIView* view in scrollView.subviews) {
    if([view isKindOfClass:[SomeView class]]) {

        // the parent of view of scrollView (which basically matches the application frame)
        CGRect f = self.view.frame; 
        // adjust our frame to match the scroll view's content offset
        f.origin.y = _scrollView.contentOffset.y;

        CGRect r = [self.view convertRect:view.frame toView:self.view];

        if(CGRectIntersectsRect(f, r)) {
            // view is visible
        }
    }
}
于 2013-05-26T06:04:20.340 回答
1

如果您主要担心释放不在视图层次结构中的对象,则可以测试它是否具有超级视图,如下所示:

if (myView.superview){
 //do something with myView because you can assume it is on the screen
}
else {
 //myView is not in the view hierarchy
}
于 2013-01-07T23:46:02.730 回答
0

我最近不得不检查我的视图是否在屏幕上。这对我有用:

CGRect viewFrame = self.view.frame;
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];

// We may have received messages while this tableview is offscreen
if (CGRectIntersectsRect(viewFrame, appFrame)) {
    // Do work here
}
于 2012-08-08T00:15:39.850 回答