更清晰的方法是不使用UIInterfaceOrientation来进行旋转。
为什么呢?因为这样你的界面或视图1就会实际旋转。这意味着在移除视图2后,你必须将其旋转回来才能重新显示它。
为了弥补这个问题,我简单地订阅了UIDeviceOrientation通知。微妙的不同之处在于,视图1无需支持自动旋转即可使此方法工作。在viewDidLoad中输入以下代码:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil]
然后只需定义detectOrientation方法:
-(void) detectOrientation {
if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) ||
([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {
} else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) {
}
}
现在你的两个视图都不需要支持自动旋转!但在加载视图2之前,你需要执行一个变换:
-(void) transformView2ToLandscape {
NSInteger rotationDirection;
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
if(currentOrientation == UIDeviceOrientationLandscapeLeft){
rotationDirection = 1;
}else {
rotationDirection = -1;
}
CGRect myFrame = CGRectMake(0, 0, 480, 300);
CGAffineTransform transform = [[self view2] transform];
transform = CGAffineTransformRotate(transform, degreesToRadians(rotationDirection * 90));
[[self view2] setFrame: myFrame];
CGPoint center = CGPointMake(myFrame.size.height/2.0, myFrame.size.width/2.0);
[[self view2] setTransform: transform];
[[self view2] setCenter: center];
}
这就是我在不支持自动旋转的视图中交换视图或旋转的方法。