显示视图时处理方向

发布于 2024-12-03 09:06:17 字数 379 浏览 0 评论 0原文

当方向改变时,我对 UIView 进行了一些更改。这很好用。在手机方向已切换后添加视图时会出现问题。这会导致不会调用任何旋转方法,因此不会给我进行更改的机会。

处理这个问题的正确方法是什么,可能是在 ViewDidLoad 中?我能够检测到此时的当前方向吗?

请记住,我需要进行一些小的更改,所以我不想加载不同的笔尖或类似的东西

非常感谢:)

编辑*只是为了澄清事情:正如我提到的,视图是当设备方向改变时,甚至还没有实例化。方向变为横向 ->用户单击显示另一个视图的按钮 ->创建并显示这个新视图,但其默认位置是纵向 ->当显示视图时,我在 willAnimateRotationToInterfaceOrientation 方法中重新排列的元素位于错误的位置。

I have a few changes that get made to a UIView when the orientation changes. This works fine. The problem arises when adding the view after the phones orientation is already switched. This causes none of the rotate methods to be called and therefor does not give me the opportunity to make changes.

What would be the correct way of handling this, probably in ViewDidLoad? Would I be able to detect the current orientation at that point?

Bare in mind that its a few minor changes that I would need to make, so I dont want to load a different nib or anything like that

Thanks you very much :)

EDIT* just to clear things up: As I mentioned, the view is not even instantiated yet when the device orientation changes. The orientation changes to landscape -> the user clicks a button that shows another view -> this new view gets created and shown, but its default positioning is for portrait orientation -> when the view is shown, the elements I rearrange in the willAnimateRotationToInterfaceOrientation method are in the wrong position.

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

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

发布评论

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

评论(1

时光瘦了 2024-12-10 09:06:17

通常,我将用户旋转设备(主要是操作视图帧)时发生的动画放在 willAnimateToInterfaceOrientation 方法中。在它的骨架形式中,它看起来像这样:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                         duration:(NSTimeInterval)duration
{
    //NSLog(@"willAnimateRotationToInterfaceOrientation: %d", toInterfaceOrientation);

    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
    {
        // portrait
    }
    else
    {
        // landscape
    }
}

编辑:在我需要记住设备旋转以供将来使用的情况下,我在我的视图控制器类中设置了一个名为 currentOrientation (类型 int)的 ivar,然后执行以下操作

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    //NSLog(@"shouldAutorotateToInterfaceOrientation: %d", toInterfaceOrientation);

    if (toInterfaceOrientation == UIDeviceOrientationPortrait || toInterfaceOrientation == UIDeviceOrientationPortraitUpsideDown ||
        toInterfaceOrientation == UIDeviceOrientationLandscapeLeft || toInterfaceOrientation == UIDeviceOrientationLandscapeRight)
    {
        currentOrientation = toInterfaceOrientation;
    }

    return YES;
}

:在视图控制器中运行方法,我知道设备处于哪个方向。

Typically I put the animations that occur when the user rotates the device (manipulating the frames of views mostly) in the willAnimateToInterfaceOrientation method. In it's skeleton form it would look like this:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                         duration:(NSTimeInterval)duration
{
    //NSLog(@"willAnimateRotationToInterfaceOrientation: %d", toInterfaceOrientation);

    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
    {
        // portrait
    }
    else
    {
        // landscape
    }
}

EDIT: In situations where I need to remember the device rotation for future use, I set up an ivar in my view controller class called currentOrientation (type int), and then do this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    //NSLog(@"shouldAutorotateToInterfaceOrientation: %d", toInterfaceOrientation);

    if (toInterfaceOrientation == UIDeviceOrientationPortrait || toInterfaceOrientation == UIDeviceOrientationPortraitUpsideDown ||
        toInterfaceOrientation == UIDeviceOrientationLandscapeLeft || toInterfaceOrientation == UIDeviceOrientationLandscapeRight)
    {
        currentOrientation = toInterfaceOrientation;
    }

    return YES;
}

Then while running the methods in the view controller, I know which orientation the device is in.

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