[英]Hide UIView on device rotation - doesn't work when device is horizontal
旋轉設備時,我試圖在視圖控制器中隱藏圖像。 我在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發送一個通知,應用程序委托隱藏bannerView。 但是,將設備平放在桌子上時,會顯示圖像。 垂直握住設備但水平放置時,效果很好...奇怪。
這是發送通知的代碼:
- (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;
}
}
您是在發布通知時發生錯誤的。
將在方向更改發生之前調用willAnimateRotationToInterfaceOrientation
(因此方法名稱中的“ will”)。 因此,如果我們要從縱向轉到橫向,則當前方向可能仍會報告為縱向(可能不,取決於具體情況)。
現在, willAnimate...
調用返回toInterfaceOrientation
即將發生的方向。
當您收到willAnimate
...調用時觸發您的通知,並且在該通知調用內[[UIDevice currentDevice]orientation]
: 它將返回portrait 。 不要在通知方法中請求方向,而應該傳遞willAnimate
調用中提供的方向。
如果不清楚,則在旋轉更改之前調用一句話摘要: willAnimateRotationToInterfaceOrientation
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.