Я пытаюсь найти вызов метода, который я могу защелкнуть, как только изменилась ориентация устройства. Есть ли что-то такое же, что didRotateFromInterfaceOrientation, которое не устарело?
Существует ли метод, не поддерживающий устаревание, который соответствует didRotateFromInterfaceOrientation
Ответ 1
Как и в iOS 8, все UIViewControllers наследуют протокол UIContentContainer, один из методов которого - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
, который вы можете (упрощенно) переопределить следующим образом:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Stuff you used to do in willRotateToInterfaceOrientation would go here.
// If you don't need anything special, you can set this block to nil.
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Stuff you used to do in didRotateFromInterfaceOrientation would go here.
// If not needed, set to nil.
}];
}
Вы заметите, что нет ничего конкретного в ориентации, которая по дизайну; Вращения просмотра iOS представляют собой, по существу, состав конкретной операции матрицы преобразования (изменение размера изображения из одного аспекта в другой является лишь конкретным случаем операции общего изменения размера, в которой известны заранее заданные размеры источника и цели) и операция матрицы вращения (который обрабатывается ОС).
Ответ 2
Swift 3 версия ответа fullofsquirrels:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: { context in
context.viewController(forKey: UITransitionContextViewControllerKey.from)
// Stuff you used to do in willRotateToInterfaceOrientation would go here.
// If you don't need anything special, you can set this block to nil.
}, completion: { context in
// Stuff you used to do in didRotateFromInterfaceOrientation would go here.
// If not needed, set to nil.
})
}
Ответ 3
Вы всегда можете зарегистрироваться для UIDeviceOrientationDidChangeNotification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification object:nil];
- (void) didRotate:(id)sender
{
UIInterfaceOrientation io = [[UIApplication sharedApplication] statusBarOrientation];
...