问题
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
In my "viewDidLoad" method
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
Then I call this for the rotation:
- (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];
}
}
回答1:
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];
}
And similar in the other direction.
Bear in mind that removing from superview releases so you need to have both views externally retained.
来源:https://stackoverflow.com/questions/6464569/changing-uiviews-during-uiinterfaceorientation-on-ipad