0

当设备旋转时,我试图在视图控制器中隐藏图像。我在 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;
}
}

PlayerViewController 发送通知,应用程序委托隐藏横幅视图。但是,当设备平放在桌子上时,图像会显示。当设备垂直但水平放置时工作正常,图像出现......奇怪。

以下是发送通知的代码:

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

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

任何想法为什么会发生这种奇怪的行为?

只需一点点更多信息。在模拟器中,图像显示设备处于倒置方向时,即使我有:

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

1 回答 1

0

您的错误可能是由于您发布通知的时间而发生的。

willAnimateRotationToInterfaceOrientation在方向改变发生之前调用(因此方法名称中的“will”)。因此,如果我们要从纵向变为横向,则当前方向可能仍会报告为纵向(可能不是,这取决于)。

现在,willAnimate...调用返回toInterfaceOrientation- 将要发生的方向

当您收到willAnimate... 呼叫时触发您的通知,并且在该通知呼叫中[[UIDevice currentDevice]orientation]这将返回 Portrait。与其在通知方法中请求方向,不如传递willAnimate调用中提供的方向。

如果不清楚,一句话总结:在轮换改变之前willAnimateRotationToInterfaceOrientation被调用。

于 2011-09-26T00:09:07.107 回答