Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Animating Incorrect Orientation at rootViewController?

Context: I am trying to perform a custom animation from a normal UIViewController.view to a UISplitViewController.view. The animation should show from Left to Right.

I set self.window.rootViewController = viewController where viewController is a normal UIViewController.

Once the user swipe, the following gets called:

UIView *theWindow = [viewController.view superview];

[viewController.view removeFromSuperview];
[theWindow addSubview:self.splitViewController.view];


CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

[[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];

When the device is in a portrait mode, everything went perfectly. However, when the device is in the landscape mode, the transition animation performs as if the device is still in the portrait mode. For example: Instead of coming in from the left, it comes in from the bottom. The orientation of both the views are completely correct. Only the transition is weird.

like image 369
Byte Avatar asked Mar 23 '12 16:03

Byte


1 Answers

So, I've been playing around with your code (I did a best-guess reconstruction of how you have your project set up), and I was able to get the desired effect with the following code. I tried setting the frames and bounds of layers, of views, or sublayers, etc., and none of that worked. CATransform3D's, CATransform3DRotates, etc also weren't doing the trick. I also Googled for a solid hour. Either no one has had your issue, or no one has solved your issue. Regardless, this solution works, and unless some Core Animation guru can provide a better solution, you're welcome to this:

- (void)swipeGesture:(UISwipeGestureRecognizer *)sender {

UIView *theWindow = [self.viewController.view superview];

UIInterfaceOrientation interfaceOrientation = self.viewController.interfaceOrientation;
NSString *subtypeDirection;
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
    subtypeDirection = kCATransitionFromTop;
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    subtypeDirection = kCATransitionFromBottom;
}
else {
    subtypeDirection = kCATransitionFromLeft;
}

[self.viewController.view removeFromSuperview];
[theWindow addSubview:self.splitViewController.view];

CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:subtypeDirection];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

[[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];
}
like image 84
jmstone617 Avatar answered Oct 16 '22 11:10

jmstone617