简体   繁体   English

在设备旋转时隐藏UIView-设备水平时不起作用

[英]Hide UIView on device rotation - doesn't work when device is horizontal

I'm trying to hide an image in a view controller when the device is rotated. 旋转设备时,我试图在视图控制器中隐藏图像。 I'm posting a notification in PlayerViewController and am listening for it in the app delegate, which is responsible for the bannerView: 我在PlayerViewController中发布了一个通知,并在负责bannerView的应用程序委托中监听该通知:

- (void)orientationChanged:(NSNotification *)notification {     

  UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

if ((orientation == UIDeviceOrientationLandscapeLeft) ||
    (orientation == UIDeviceOrientationLandscapeRight)) {

    bannerView.hidden = ([[self.navigationController visibleViewController] isKindOfClass:[PlayerViewController class]]) ? YES : NO;

} else {
    bannerView.hidden = NO;
}
}

The PlayerViewController sends a notification and the app delegate hides the bannerView. PlayerViewController发送一个通知,应用程序委托隐藏bannerView。 However, when the device is laid flat on a table, the image shows. 但是,将设备平放在桌子上时,会显示图像。 Works fine when the device is held vertically but horizontally the image appears... odd. 垂直握住设备但水平放置时,效果很好...奇怪。

Here is the code to send the notification: 这是发送通知的代码:

 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                     duration:(NSTimeInterval)duration {

if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
 ... hide other stuff in this view controller
 }

Any ideas why this odd behavior is occurring? 任何想法为什么这种奇怪的行为发生?

Just one tidbit more information. 只需一点点更多信息。 In the simulator the image shows when the device is in upside-down orientation, even though I have: 在模拟器中,该图像显示了设备何时处于上下颠倒的方向,即使我有:

- (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation
 {
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationPortrait) {
    return YES;
} else {
    return NO;
}
}

Your error might be happening because of when you're posting the notification. 您是在发布通知时发生错误的。

willAnimateRotationToInterfaceOrientation is called before the orientation change takes place (hence the "will" in the method name). 方向更改发生之前调用willAnimateRotationToInterfaceOrientation (因此方法名称中的“ will”)。 So if we're going from portrait to landscape, the current orientation may still be reported as portrait (it may not, it depends). 因此,如果我们要从纵向转到横向,则当前方向可能仍会报告为纵向(可能不,取决于具体情况)。

Now, the willAnimate... call returns the toInterfaceOrientation - the orientation that is going to happen. 现在, willAnimate...调用返回toInterfaceOrientation 即将发生的方向。

You trigger your notification when you receive the willAnimate ... call, and inside that notification call [[UIDevice currentDevice]orientation] : which will return portrait . 当您收到willAnimate ...调用时触发您的通知,并且在该通知调用内[[UIDevice currentDevice]orientation]它将返回portrait Instead of requesting the orientation in your notification method you should instead pass the orientation provided in the willAnimate call. 不要在通知方法中请求方向,而应该传递willAnimate调用中提供的方向。

If that wasn't clear, the one sentence summary: willAnimateRotationToInterfaceOrientation is called before the rotation changes . 如果不清楚,则在旋转更改之前调用一句话摘要: willAnimateRotationToInterfaceOrientation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM