是否存在与didRotateFromInterfaceOrientation匹配的非弃用方法

Bra*_*don 11 orientation-changes ios

我试图找到一个方法调用,一旦设备的方向发生变化,我就可以锁定它.是否有与didRotateFromInterfaceOrientation相同的东西没有被弃用?

ful*_*els 27

从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.

    }];
}
Run Code Online (Sandbox Code Playgroud)

你会发现没有具体的方向,这是设计的; iOS视图旋转本质上是特定平移矩阵操作的组合(将视图从一个宽高比调整为另一个只是一般调整大小操作的特定情况,其中源视图大小和目标视图大小是预先知道的)和旋转矩阵操作(由操作系统处理).