我有一个带有 4 个视图控制器的故事板。1 - initialViewController 2 - PortraitViewController 3- LeftLandscapeViewController 4 - RightLandscapeViewController。
当设备旋转时,一个新的 viewcontroller 作为 childViewController 加载,最后一个 childViewController 被移除(如果存在)。
- (void)switchViews:(NSString*)storyboardId
{ __block UIViewController *lastController = _childController;
_childController = (UIViewController*)[self.storyboard instantiateViewControllerWithIdentifier:storyboardId];
[self addChildViewController:_childController];
[_parentView addSubview:_childController.view];
CGRect parentRect = _parentView.bounds;
parentRect.origin.x += parentRect.size.width;
_childController.view.frame = parentRect;
_childController.view.alpha = 0;
[UIView animateWithDuration:0.3 animations:^{
_childController.view.frame = _parentView.bounds;
_childController.view.alpha = 1.0;
} completion:^(BOOL finished) {
if (lastController) // removes previous viewController
{
[lastController.view removeFromSuperview];
[lastController removeFromParentViewController];
}
}];
}
-(void)updateOrientationView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
// ---> LANDSCAPE ROTATION
if(UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeRightView)
{
if (deviceOrientation == UIDeviceOrientationLandscapeRight)
{
if([lastOrientation isEqual:@"landscapeRight"])
{
//NSLog(@"ROTATE TO LANDSCAPE RIGHT VIEW");
}else{
[self switchViews:@"landscapeRight"];
lastOrientation = @"landscapeRight";
NSLog(@"ROTATE TO LANDSCAPE RIGHT VIEW");
}
}
if (deviceOrientation == UIDeviceOrientationLandscapeLeft)
{
if([lastOrientation isEqual:@"landscapeLeft"])
{
//
}else{
[self switchViews:@"landscapeLeft"];
lastOrientation = @"landscapeLeft";
NSLog(@"ROTATE TO LANDSCAPE LEFT VIEW");
}
}
}
if(UIDeviceOrientationIsPortrait(deviceOrientation) && !isShowingPortraitView)
{
// ---> PORTRAIT
if (deviceOrientation == UIDeviceOrientationPortrait)
{
if([lastOrientation isEqual: @"portrait"])
{
//
}else{
[self switchViews:@"portrait"];
lastOrientation = @"portrait";
NSLog(@"ROTATE TO PORTRAIT VIEW");
}
}
// ---> PORTRAIT UPSIDE DOWN
if (deviceOrientation == UIDeviceOrientationPortraitUpsideDown)
{
//lastOrientation = @"portraitUpsideDown";
// do nothing
}
}
}
当我在我的设备上运行我的应用程序时,在将其横向然后纵向旋转大约 8 次后,我的应用程序非常非常缓慢。由于我要删除以前的子视图,因此我认为不应该是正确的吗?我想知道我的视图控制器是否被解雇/删除,或者这里发生了什么?
感谢您的任何指导