当我们开始旋转设备时以及旋转完成后将调用什么方法

发布于 2024-12-04 16:21:53 字数 96 浏览 1 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深巷少女 2024-12-11 16:21:53

来自 Apple 文档:

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

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

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

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

在此处查看更多信息:UIViewController 类参考->响应视图轮换事件

注意:
此功能已弃用,请参阅这篇文章

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

ATTENTION:
This is deprecated, see this post

诗酒趁年少 2024-12-11 16:21:53

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

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

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

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

更多信息可在 文档

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

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

i.e.

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

More info is availablein the Docs.

暖树树初阳… 2024-12-11 16:21:53

上述所有方法(@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.

    }];
}

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

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.

In iOS8 or later you can use the below method.

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

    }];
}
独夜无伴 2024-12-11 16:21:53

环球银行金融电信协会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
    }
}

SWIFT 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
    }
}
决绝 2024-12-11 16:21:53

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

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

有四种显示模式:

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

BUT这个方法将NEVER返回Automatic。

In the UISplitViewController protocol, the new method for iOS8 is

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

There are four display modes:

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

BUT this method will NEVER return Automatic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文