是否有一个未弃用的方法匹配 didRotateFromInterfaceOrientation

Is there a non-deprecated method that matches didRotateFromInterfaceOrientation

我试图找到一个方法调用,一旦设备的方向发生变化,我就可以锁定它。是否有与 didRotateFromInterfaceOrientation 相同但未弃用的内容?

您可以随时注册 UIDeviceOrientationDidChangeNotification

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didRotate:)
                                                 name:UIDeviceOrientationDidChangeNotification object:nil];

- (void) didRotate:(id)sender
{
    UIInterfaceOrientation io = [[UIApplication sharedApplication] statusBarOrientation];


    ...

从 iOS 8 开始,所有 UIViewController 都继承了 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 视图旋转本质上是特定平移矩阵操作的组合(将视图从一个宽高比调整到另一个只是一般调整大小操作的一种特殊情况,其中源视图和目标视图大小是事先已知的)和旋转矩阵运算(由 OS 处理)。

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.
        })
}