I’m trying to add an effect to a switch between 2 views. The switch happen just rotating the device. So far I’m able to apply the effect when from portrait go to landscape, but not viceversa.
Here’s my code:
-(void)orientationChanged:(NSNotification *)object{
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if (deviceOrientation == UIInterfaceOrientationPortrait)
{
/* If I run with this block, I have a Exc Bad Access Error and can't run, so I put it
under comment
[UIView transitionWithView:self.view duration:1.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self.view addSubview:self.portraitView];
} completion:NULL];
*/
self.view = self.portraitView;
//[self dismissModalViewControllerAnimated:YES];
}
else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || deviceOrientation ==
UIInterfaceOrientationLandscapeRight)
{
[UIView transitionWithView:self.view duration:1
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self.view addSubview:self.landscapeView];
} completion:NULL];
[self dismissModalViewControllerAnimated:NO];
}
}
In this way I have the desired effect when passing from Portrait to Landscape, but when I put the device back to Portrait it doesn’t load the portraitView, the landscapeView remains on.
Hope somebody can help me.
EDIT:
I just edited my above code in this way:
-(void)orientationChanged:(NSNotification *)object{
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if (deviceOrientation == UIDeviceOrientationPortrait)
{
if (self.isViewLoaded && self.view.window)
{
NSLog(@"Portrait1");
[UIView transitionWithView:self.view duration:1
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
NSLog(@"Portrait2");
[self.view.window addSubview:self.portraitView];
} completion:NULL];
[self dismissModalViewControllerAnimated:YES];
}
}
else if (deviceOrientation == UIDeviceOrientationLandscapeLeft || deviceOrientation ==
UIDeviceOrientationLandscapeRight)
{
[UIView transitionWithView:self.view duration:1
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self.view addSubview:self.landscapeView];
} completion:NULL];
[self dismissModalViewControllerAnimated:NO];
NSLog(@"Landscape");
}
}
And the effect works fine. But I have the big issue that when I switch back from Landscape to Portrait the landscapeView remains ON the portraitView. How could I dismiss it?
Referring to your updated code, when your device orientation becomes UIDeviceOrientationPortrait, you are making the mistake of adding portraitView as a subview to self.view.window instead of self.view.
Also, what is the purpose behind [self dismissModalViewControllerAnimated:NO]? You haven’t actually presented a modal view controller anywhere, so it doesn’t serve any purpose.