在iPad上的UIInterfaceOrientation期间更改UIViews [英] Changing UIViews during UIInterfaceOrientation on iPad
问题描述
我试图更改旋转角度,因为从肖像到风景,我的观点必须有显着差异。现在,我正在使用的代码可以运行一次,然后在尝试向后旋转时应用程序冻结。任何一个方向都没有影响。例如:如果我在风景中并旋转以纵向,则一切正常,直到我旋转回风景,然后它冻结,并且绝对不执行任何操作。
I am trying to change views on rotation because my views have to be significantly different from portrait to landscape. Now the code I am using works once then the app freezes when trying to rotate back. Either direction does not make a difference. For example: If I am in Landscape and rotate to portrait everything works great until I rotate back to landscape then it freezes and does absolutely nothing.
这是我用来实现此目的的代码
Here is the code I am using to achieve this
在我的 viewDidLoad方法中
In my "viewDidLoad" method
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
然后我将其称为轮换:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (void)didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if ((orientation == UIDeviceOrientationLandscapeLeft) ||
(orientation == UIDeviceOrientationLandscapeLeft))
{
// present the other viewController, it's only viewable in landscape
[self.view addSubview:landScapeView];
}
if ((orientation == UIDeviceOrientationLandscapeRight) ||
(orientation == UIDeviceOrientationLandscapeRight))
{
// present the other viewController, it's only viewable in landscape
[self.view addSubview:landScapeView];
}
else if ((orientation == UIDeviceOrientationPortrait ||
(orientation == UIDeviceOrientationPortrait))
{
// get rid of the landscape controller
[self.view addSubview:portrait];
}
else if ((orientation == UIDeviceOrientationPortraitUpsideDown ||
(orientation == UIDeviceOrientationPortraitUpsideDown))
{
// get rid of the landscape controller
[self.view addSubview:portrait];
}
}
推荐答案
您正在旋转时添加特定于方向的视图,但从不删除其他视图,因此您需要这样做。
You are adding your orientation specific view at the rotate but never removing the other one so you need.
// get rid of the landscape controller
((orientation == UIDeviceOrientationPortraitUpsideDown || orientation ==
UIDeviceOrientationPortraitUpsideDown)) {
[landScapeView removeFromSuperview];
[self.view addSubview:portrait];
}
在另一个方向上类似。
请记住,要从超级视图版本中删除,因此您需要在外部保留两个视图。
Bear in mind that removing from superview releases so you need to have both views externally retained.
这篇关于在iPad上的UIInterfaceOrientation期间更改UIViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!