当我们开始旋转设备并完成后,将调用什么方法

R. *_*ewi 26 methods rotation ipad ios

我想以编程方式检测ipad上的旋转过程.在这种情况下,我想在旋转开始时将布尔值设置为yes,并在旋转结束后将其设置为false.是否有任何方法在旋转开始并且旋转结束时调用?

Nek*_*kto 36

来自Apple Docs:

在用户界面开始旋转之前发送到视图控制器.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
Run Code Online (Sandbox Code Playgroud)

用户界面旋转后发送到视图控制器:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
Run Code Online (Sandbox Code Playgroud)

在此处查看更多:UIViewController类参考 - >响应视图旋转事件

注意: 这已被弃用,请参阅此帖子

  • 现在已弃用 (5认同)

wyz*_*207 28

对于这篇文章的新手,Nekto建议的方法已经在iOS 8中被弃用.Apple建议使用:

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
Run Code Online (Sandbox Code Playgroud)

您可以使用"尺寸"参数作为一种简单的方法来获取它是转换为纵向还是横向.

if (size.width > size.height)
{
    // Position elements for Landscape
}
else
{
    // Position elements for Portrait
}
Run Code Online (Sandbox Code Playgroud)

更多信息可在文档中找到.


Uts*_*sad 19

所有上述方法(在@Nekto的回答中)都在iOS8.0及更高版本中被弃用.来源:iOS开发者库

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

在iOS8或更高版本中,您可以使用以下方法.

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

  • 这是一个很好的答案:) (2认同)
  • 我要保存这个 (2认同)
  • 这个答案是最好的 (2认同)