UIViewが画面上に表示されているかどうかを確認するにはどうすればよいですか?
-
02-07-2019 - |
質問
UIView
(または UIView
サブクラス)が表示されている場合、それが現在画面に表示されているかどうかを確認する方法(たとえば、 、現在画面外にあるスクロールビューのセクションにありますか?
多分私が意味することをよりよく理解するために、 UITableView
には現在表示されているセルのセットを決定するためのメソッドがいくつかあります。特定の UIView
に対して同様の判断を下せるコードを探しています。
解決
これはまだ試していません。しかし、 CGRectIntersectsRect()
、-[UIView convertRect:to(from)View]
および-[UIScrollView contentOffset]
は基本的な構成要素のようですこちら。
他のヒント
UIScrollViewに表示されているUIViewを確認するために使用したものは次のとおりです。
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
}
所属していません StackOverflow