如何阻止 mainView 旋转?

发布于 2024-10-11 00:17:59 字数 755 浏览 4 评论 0原文

我有这段代码,设置为当手机旋转到横向时使时钟视图出现,然后当它返回到纵向时使其返回到主视图。但当它返回纵向时,纵向视图处于横向模式。我该如何解决这个问题?这是我的代码。

-(void)willAnimateRotationToInterfaceOrientation(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    UIInterfaceOrientation toOrientation = self.interfaceOrientation;

    if(toOrientation == UIInterfaceOrientationPortrait)
    {
        self.view = mainView;
    }

    else if(toOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        self.view = clockView;
    }

    else if (toOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        self.view = mainView;
    }

    else if(toOrientation == UIInterfaceOrientationLandscapeRight)
    {
        self.view = clockView;
    }
}

I have this code, set to make the clockView appear when the phone is rotated to landscape, and then to make it go back to the mainView when it's returned to portrait. But when it does go back to portrait, the portrait view is in landscape mode. How do i fix this? This is my code.

-(void)willAnimateRotationToInterfaceOrientation(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    UIInterfaceOrientation toOrientation = self.interfaceOrientation;

    if(toOrientation == UIInterfaceOrientationPortrait)
    {
        self.view = mainView;
    }

    else if(toOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        self.view = clockView;
    }

    else if (toOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        self.view = mainView;
    }

    else if(toOrientation == UIInterfaceOrientationLandscapeRight)
    {
        self.view = clockView;
    }
}

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

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

发布评论

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

评论(1

故笙诉离歌 2024-10-18 00:17:59

将 mainView 和 ClockView 作为视图控制器的子视图可能会更好:

// in viewDidLoad do [self.view addSubView:mainView]; [self.view addSubView:clockView]; clockView.hidden = YES;

-(void)willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if(UIInterfaceOrientationIsPortrait(toOrientation)) {
        mainView.hidden = NO;
        clockView.hidden = YES;
    }
    else {
        mainView.hidden = YES;
        clockView.hidden = NO;
    }
}

这样,两个视图都会以正确的方式自动旋转,并且您的问题应该消失。

It is probably better to have both mainView and clockView as subviews of your view controller:

// in viewDidLoad do [self.view addSubView:mainView]; [self.view addSubView:clockView]; clockView.hidden = YES;

-(void)willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if(UIInterfaceOrientationIsPortrait(toOrientation)) {
        mainView.hidden = NO;
        clockView.hidden = YES;
    }
    else {
        mainView.hidden = YES;
        clockView.hidden = NO;
    }
}

This way, boths views are automatically rotated in the right way and your problem should go away.

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