只有一个视图在 xcode 中自动旋转?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8043263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Only having one view autorotate in xcode?
提问by davis
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.
好的,所以我目前有 3 个视图,我只需要其中一个视图自动旋转到任何方向,而其余视图保持纵向。现在我的设置是一个 splashviewcontroller 淡入视图 A,而内部视图 A 是一个切换到视图 B 的按钮。我想要的是视图 B 能够旋转到任何方向。
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
当我在 splashviewcontroller 中为 shouldautorotatetointerfaceorientation 返回 YES 时,每个视图都会旋转,因为这是父视图。当我仅在飞溅视图中返回肖像时,即使其他视图返回 YES,也不会旋转。有没有办法只让视图 B 旋转?如果您可以提供代码,我愿意手动完成。谢谢
采纳答案by Cyprian
You can manually mange the rotation of any desired UIView object like so:
您可以手动管理任何所需 UIView 对象的旋转,如下所示:
EDIT:
编辑:
In the init or viewDidLoad
在 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];
}
}
回答by Mr. Crowley
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.
在目标摘要部分,支持的接口方向中,除了颠倒之外的所有项目都被选中,因此您需要做的就是转到处理视图的 .m 文件并使用这段代码。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
This did the trick for me.
这对我有用。