ios 当我们开始旋转设备时和完成后会调用什么方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7443943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:33:04  来源:igfitidea点击:

what method will called when we start to rotate device and after it finished

iosipadmethodsrotation

提问by R. Dewi

i want to detect a rotation process on ipad programmatically. In this case i want to set a boolean into yes, when the rotation will begin, and set it false after the rotation did ending. Is there any method that called when the rotation will begin and the rotation did ending?

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

回答by Nekto

From Apple Docs:

来自苹果文档:

Sent to the view controller just before the user interface begins rotating.

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

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

Sent to the view controller after the user interface rotates:

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

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

See more here: UIViewController Class Reference -> Responding to View Rotation Events

在此处查看更多信息:UIViewController Class Reference -> Responding to View Rotation Events

ATTENTION:This is deprecated, see this post

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

回答by julianwyz

For newcomers to this post, the methods suggested by Nekto have become deprecated in iOS 8. Apple suggests to use:

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

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator

You can use the "size" parameter as an easy way to get whether it is transitioning to portrait or landscape.

您可以使用“size”参数作为一种简单的方法来了解它是过渡到纵向还是横向。

i.e.

IE

if (size.width > size.height)
{
    // Position elements for Landscape
}
else
{
    // Position elements for Portrait
}

More info is availablein the Docs.

文档中提供了更多信息。

回答by Utsav Dusad

All the above methods(in the answer by @Nekto) are deprecated in iOS8.0 and later versions. Source: iOS Developer Library

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

As of iOS 8, all rotation-related methods are deprecated. Instead, rotations are treated as a change in the size of the view controller's view and are therefore reported using the viewWillTransitionToSize:withTransitionCoordinator: method. When the interface orientation changes, UIKit calls this method on the window's root view controller. That view controller then notifies its child view controllers, propagating the message throughout the view controller hierarchy.

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

In iOS8 or later you can use the below method.

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

    }];
}

回答by lenooh

SWIFT 5:

快速 5:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    coordinator.animate(alongsideTransition: { _ in
        // code at start of rotation
    }) { _ in
        // code at end of rotation
    }
}

回答by mtet88

In the UISplitViewController protocol, the new method for iOS8 is

在 UISplitViewController 协议中,iOS8 的新方法是

- (void)splitViewController:(UISplitViewController *)svc willChangeToDisplayMode:(UISplitViewControllerDisplayMode)displayMode

There are four display modes:

有四种显示模式:

typedef enum UISplitViewControllerDisplayMode : NSInteger {
  UISplitViewControllerDisplayModeAutomatic,
  UISplitViewControllerDisplayModePrimaryHidden,
  UISplitViewControllerDisplayModeAllVisible,
  UISplitViewControllerDisplayModePrimaryOverlay,
} UISplitViewControllerDisplayMode;

BUTthis method will NEVERreturn Automatic.

但是这种方法永远不会返回自动。