UIWindow根据当前方向旋转

UIWindow根据当前方向旋转

本文介绍了iPhone - UIWindow根据当前方向旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用添加额外的UIWindow。
我的主窗口正确旋转,但我添加的这个附加窗口不会旋转。

I am adding an additional UIWindow to my app.My main window rotates correctly, but this additional window I have added does not rotate.

根据当前设备旋转UIWindow的最佳方法是什么方向?

What is the best way to rotate a UIWindow according to the current device orientation?

推荐答案

你需要为UIWindow自己动手。

You need to roll your own for UIWindow.

听取 UIApplicationDidChangeStatusBarFrameNotification 通知,然后在状态栏更改时设置转换。

Listen for UIApplicationDidChangeStatusBarFrameNotification notifications, and then set the the transform when the status bar changes.

您可以阅读来自的当前方向 - [UIApplication statusBarOrientation] ,并计算如下变换:

You can read the current orientation from -[UIApplication statusBarOrientation], and calculate the transform like this:

#define DegreesToRadians(degrees) (degrees * M_PI / 180)

- (CGAffineTransform)transformForOrientation:(UIInterfaceOrientation)orientation {

    switch (orientation) {

        case UIInterfaceOrientationLandscapeLeft:
            return CGAffineTransformMakeRotation(-DegreesToRadians(90));

        case UIInterfaceOrientationLandscapeRight:
            return CGAffineTransformMakeRotation(DegreesToRadians(90));

        case UIInterfaceOrientationPortraitUpsideDown:
            return CGAffineTransformMakeRotation(DegreesToRadians(180));

        case UIInterfaceOrientationPortrait:
        default:
            return CGAffineTransformMakeRotation(DegreesToRadians(0));
    }
}

- (void)statusBarDidChangeFrame:(NSNotification *)notification {

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    [self setTransform:[self transformForOrientation:orientation]];

}

根据您的窗口大小,您可能需要更新框架也是如此。

Depending on your window´s size you might need to update the frame as well.

这篇关于iPhone - UIWindow根据当前方向旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 15:34