如果我有一个可见的 UIView (或 UIView 子类),我怎么知道它当前是否在屏幕上显示(例如,相反) ,位于当前屏幕外的滚动视图的一部分中?

为了让您更好地了解我的意思, UITableView 有几种方法可以确定当前可见单元格的集合。我正在寻找一些代码可以对任何给定的 UIView 做出类似的决定。

有帮助吗?

解决方案

尚未尝试任何此类操作。但是 CGRectIntersectsRect() - [UIView convertRect:to(from)View] - [UIScrollView contentOffset] 似乎是你的基本构建块这里。

其他提示

以下是我用来检查哪些UIViews在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
        }
    }
}

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

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
}

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

if (myView.superview){
 //do something with myView because you can assume it is on the screen
}
else {
 //myView is not in the view hierarchy
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top