Обнаружение поворота на пейзаж вручную
Я работаю над iPhone-приложением на основе UITabBarController и UIViewControllers для каждой страницы. Приложение должно работать только в портретном режиме, поэтому каждый делегат диспетчера представлений + приложение-приложение идет с этой строкой кода:
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Существует один контроллер представлений, в котором я хотел бы открыть UIImageView, когда iPhone будет привязан к ландшафтному экрану. Дизайн изображения выглядит ландшафтно, хотя ширина и высота - 320x460 (поэтому его портрет).
Как можно/должен ли я обнаруживать этот тип вращения вручную, только в этом конкретном контроллере представления, причем у него есть автоматическое вращение на весь вид?
Томас
ОБНОВЛЕНИЕ:
Спасибо!
Я добавил этого слушателя в viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:)name:UIDeviceOrientationDidChangeNotification object:nil];
didRotate выглядит так:
- (void) didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft)
{
//your code here
}
}
Ответы
Ответ 1
Мне нужно было в старом проекте - надеюсь, что он все еще работает...
1) Зарегистрировать уведомление:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(detectOrientation)
name:UIDeviceOrientationDidChangeNotification
object:nil];
2) Затем вы можете проверить вращение при изменении:
-(void) detectOrientation {
if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) ||
([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {
[self doLandscapeThings];
} else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) {
[self doPortraitThings];
}
}
Надеюсь, что это поможет!
Ответ 2
лучший код для ответа Comic Sans будет ниже.. Его код не всегда срабатывает правильно (только 80% времени в моем тестировании)
-(void) detectOrientation {
if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
[self setupForLandscape];
} else if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
[self setupForPortrait];
}
}