I have an storyboard with 4 view controllers. 1 - initialViewController 2 - PortraitViewController 3- LeftLandscapeViewController 4 - RightLandscapeViewController.
When the device rotates, a new viewcontroller is loaded as a childViewController and the last childViewController is removed (if present).
- (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
}
}
}
When i run my app on my device, after rotating it landscape and then portrait about 8 times my app is very, very slugish. Am I thinkgin correct that is should not since I am removing the previous child view? I am wondering if my viewcontrollers are being dismissed/removed, or is something else going on here?
Thanks for any guidance