如果我有一个可见的UIView (或UIView子类),我如何判断它当前是否显示在屏幕上(而不是,例如,在当前屏幕外的滚动视图中)?
为了让您更好地理解我的意思,UITableView提供了几种方法来确定当前可见的单元格。我正在寻找一些代码,可以对任何给定的UIView做出类似的判断。
发布于 2008-09-28 02:35:55
还没试过这些。但在这里,CGRectIntersectsRect()、-[UIView convertRect:to(from)View]和-[UIScrollView contentOffset]似乎是您的基本构件。
发布于 2013-05-26 14:04:20
下面是我用来检查哪些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
}
}
}发布于 2013-01-08 07:46:03
如果您主要担心释放不在视图层次结构中的对象,则可以测试它是否具有superview,如下所示:
if (myView.superview){
//do something with myView because you can assume it is on the screen
}
else {
//myView is not in the view hierarchy
}https://stackoverflow.com/questions/142282
复制相似问题