xcode 我如何知道视图是否可见?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11498115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I know if a view is visible or not?
提问by Sahil Chaudhary
Say I have two view controllers: xVC and yVC. I have used the shake API and and have used the methods -(void)motionBegan
,-(void)motionEnded:
and -(void)motionCancelled
in xVC. What happens is when the device is shaken, it fires a simple animation. Now the thing is that this animation is fired even when the I have yVC open that is, when yVS.view
has been added as the subview. What I am looking for is some if condition which I can use in -(void)motionEnded:
like this:
假设我有两个视图控制器:xVC 和 yVC。我使用了shake API,并且使用了方法-(void)motionBegan
,-(void)motionEnded:
以及-(void)motionCancelled
在xVC中。发生的情况是,当设备晃动时,它会触发一个简单的动画。现在的问题是,即使我打开了 yVC,yVS.view
即已添加为子视图,也会触发此动画。我正在寻找的是一些 if 条件,我可以-(void)motionEnded:
像这样使用它:
if(yVC == nil)
{
//trigger animation
}
By that I mean that the shake shouldn't work when yVC is visible. How do I do that? Please help.
我的意思是,当 yVC 可见时,震动不应该起作用。我怎么做?请帮忙。
回答by Chris Trahey
The general advice I have seen and used is to ask a view if it has a non-nil window
property:
我所看到和使用的一般建议是询问视图是否具有非零window
属性:
if( ! yVC.view.window) {
// trigger animation
}
But note that this doesn't alwaysequate with being visible; though in most apps it's about as good as you can performantly get (the basic case where it's not accurate is when a different view completely obscures it, but this may still satisfy your needs)
但请注意,这并不总是等同于可见;尽管在大多数应用程序中,它与您的性能一样好(它不准确的基本情况是当不同的视图完全模糊它时,但这仍然可以满足您的需求)
回答by Shantanu
Add this to both of your view controllers:
将此添加到您的两个视图控制器中:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
visible = YES;
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
visible = NO;
}
Now, just check the variable isVisible of both the view controllers and trigger your animation likewise.
现在,只需检查两个视图控制器的变量 isVisible 并同样触发您的动画。
回答by Daniel Larsson
The previous answers all work to some degree, but fail to take modally presented view controllers into account. If view controller A presents view controller B, of the previous answers will tell you that A is still visible. If you, like me, want to know whether or not the view is actually visible (and not just a part of the view hierarchy), I would suggest also checking the presentedViewController
property:
以前的答案在某种程度上都有效,但没有考虑到模态呈现的视图控制器。如果视图控制器 A 呈现视图控制器 B,则前面的答案会告诉您 A 仍然可见。如果您像我一样想知道视图是否实际可见(而不仅仅是视图层次结构的一部分),我建议还检查presentedViewController
属性:
if (self.isViewLoaded && [self.view window] && !self.presentedViewController) {
// User is looking at this view and nothing else
}
This works since presentedViewController
will be non-nil whenever the current view controller ORany of its ancestors are currently presenting another view controller.
这工作,因为presentedViewController
将非空每当当前视图控制器或任何始祖目前呈现另一个视图控制器。