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

前端之家收集整理的这篇文章主要介绍了iphone – 只有一个视图在xcode中自动旋转?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好的,所以我目前有3个视图,我只需要其中一个自动旋转到任何方向,而其余的保持纵向.现在我的设置是一个splashviewcontroller淡入视图A,内部视图A是一个切换到视图B的按钮.我想要的是视图B能够旋转到任何方向.

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

解决方法

你可以手动管理任何所需的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);

    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,480);

        [self.view setTransform:landscapeTransform];

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

        CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));

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


        [self.view setTransform:landscapeTransform];

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

        CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(270));

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

        [self.view setTransform:landscapeTransform];
    }

}

猜你在找的Xcode相关文章