xcode 中只有一个视图自动旋转?

发布于 2024-12-14 07:37:04 字数 287 浏览 4 评论 0原文

好吧,我目前有 3 个视图,我只需要其中一个即可自动旋转到任何方向,而其余的则保持纵向。现在我的设置是一个splashviewcontroller淡入视图A,内部视图A是一个切换到视图B的按钮。我想要的只是视图B能够旋转到任何方向。

当我在splashviewcontroller中为shouldautorotatetointerfaceorientation返回YES时,每个视图都会旋转,因为这是父视图。当我仅在启动视图中返回纵向时,即使其他视图返回“是”,也不会旋转。有没有办法只让视图 B 旋转?如果您可以提供代码,我愿意手动完成。谢谢

Ok so I currently have 3 views and I need only one of them to autorotate to any orientation while the rest stay in portrait. Right now my set up is a splashviewcontroller fades into view A, and inside view A is a button to switch to view B. All I want is for view B to be able to rotate to any orientation.

When I return YES for shouldautorotatetointerfaceorientation in the splashviewcontroller, every view rotates because this is the parent view. When I return portrait only in the splashview, nothing rotates even if the other views return YES. Is there a way to only have view B rotate? I'm willing to do it manually if you can provide code. Thanks

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

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

发布评论

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

评论(2

﹎☆浅夏丿初晴 2024-12-21 07:37:04

您可以手动管理任何所需 UIView 对象的旋转,如下所示:

编辑:

在 init 或 viewDidLoad 中

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotate) name:UIDeviceOrientationDidChangeNotification object:nil];

    [super viewDidLoad];
}

#define degreesToRadian(x) (M_PI * (x) / 180.0)

-(void)rotate{

    self.view.bounds = CGRectMake(0, 0, 0, 0);

    if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortrait){


        CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(0));
        landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);

        self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 320, 480);

        [self.view setTransform:landscapeTransform];


    } else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortraitUpsideDown){


        CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(180));
        landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);

        self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 320, 480);

        [self.view setTransform:landscapeTransform];

    } else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight){

        CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));

         landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
         self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 480, 320); 


        [self.view setTransform:landscapeTransform];

    }else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft){

        CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(270));

        landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
        self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 480, 320); 

        [self.view setTransform:landscapeTransform];
    }

}

You can manually mange the rotation of any desired UIView object like so:

EDIT:

In the init or viewDidLoad

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotate) name:UIDeviceOrientationDidChangeNotification object:nil];

    [super viewDidLoad];
}

#define degreesToRadian(x) (M_PI * (x) / 180.0)

-(void)rotate{

    self.view.bounds = CGRectMake(0, 0, 0, 0);

    if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortrait){


        CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(0));
        landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);

        self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 320, 480);

        [self.view setTransform:landscapeTransform];


    } else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortraitUpsideDown){


        CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(180));
        landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);

        self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 320, 480);

        [self.view setTransform:landscapeTransform];

    } else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight){

        CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));

         landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
         self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 480, 320); 


        [self.view setTransform:landscapeTransform];

    }else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft){

        CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(270));

        landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
        self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 480, 320); 

        [self.view setTransform:landscapeTransform];
    }

}
玩套路吗 2024-12-21 07:37:04

在目标摘要部分中,支持的界面方向除颠倒之外的所有项目均已选择,因此您需要做的就是转到处理视图的 .m 文件并使用这段代码。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

这对我来说很有效。

Inside targets summary section, Supported Interface Orientation all items are selected except for Upside Down, so all you need to do is go to your .m file that handles the view and use this piece of code.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

This did the trick for me.

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