1

在我的 iOS 应用程序中,我根据比例设置了所有内容。它根据设备的宽度和高度以编程方式创建我的所有图像。我在网上找到我需要使用的:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                         duration:(NSTimeInterval)duration 

但是,由于我使用屏幕宽度和高度,我需要在它旋转后调用我的重新加载。我应该使用什么功能,比如在旋转发生后触发的这个功能?

4

4 回答 4

3

来自 iOS 开发人员对 UIViewController旋转的参考:

当可见视图控制器发生旋转时,将在旋转期间调用 willRotateToInterfaceOrientation:duration:、willAnimateRotationToInterfaceOrientation:duration: 和 didRotateFromInterfaceOrientation: 方法。viewWillLayoutSubviews 方法也会在视图被其父级调整大小和定位后调用。如果在方向更改发生时视图控制器不可见,则永远不会调用旋转方法。但是,viewWillLayoutSubviews 方法在视图变得可见时被调用。您对此方法的实现可以调用 statusBarOrientation 方法来确定设备方向。

所以你得到了你需要的所有信息。要么使用,要么viewWillLayoutSubviews,如果这对你的目的来说太晚了,使用didRotateFromInterfaceOrientation:.

于 2013-08-31T03:53:16.970 回答
1

界面改变方向后调用 viewWillLayoutSubviews 方法。调用 self.view.bounds.size 将为您提供新方向的正确宽度和高度。自 iOS 5.0 起可用。请参阅 Apple 文档中的 UIViewController 类参考。

于 2013-08-31T03:56:08.277 回答
1

从 iOS 8 开始,所有与旋转相关的方法都已弃用。相反,旋转被视为视图控制器视图大小的变化,因此使用 viewWillTransitionToSize:withTransitionCoordinator:方法报告。当界面方向改变时,UIKit 在窗口的根视图控制器上调用此方法。然后,该视图控制器通知其子视图控制器,在整个视图控制器层次结构中传播消息。

来源:iOS 开发者库

下面是如何使用它的示例。

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

    }];
}
于 2016-04-11T12:12:32.667 回答
0

UIDevice 能够生成通知,在事后告诉你方向变化。请记住,为 UIDeviceOrientation 声明的枚举与为 UIInterfaceOrientation 声明的枚举并不完全相同。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

[[NSNotificationCenter defaultCenter] addObserverForName:UIDeviceOrientationDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    NSLog(@"%d",orientation);

}];
于 2013-08-31T03:42:22.697 回答