4

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

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

4

2 回答 2

4

您可以手动管理任何所需 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];
    }

}
于 2011-11-07T22:12:51.763 回答
0

在目标摘要部分,Supported Interface Orientation 除了 Upside Down 之外,所有项目都被选中,所以您需要做的就是转到处理视图的 .m 文件并使用这段代码。

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

这对我有用。

于 2013-01-14T12:05:41.527 回答