上下文:我正在尝试执行从普通 UIViewController.view
到 UISplitViewController.view
的自定义动画。动画应该从左到右显示。
我设置了 self.window.rootViewController = viewController
,其中 viewController
是一个普通的 UIViewController
。
一旦用户滑动,就会调用以下内容:
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"];
当设备处于纵向模式时,一切都很顺利。但是,当设备处于横向模式时,过渡动画的执行就像设备仍处于纵向模式一样。例如:不是从左边进来,而是从下面进来。两种观点的方向都是完全正确的。只有过渡很奇怪。
最佳答案
所以,我一直在研究您的代码(我对您的项目设置方式进行了最佳猜测重构),并且我能够通过以下代码获得预期的效果。我尝试设置层、 View 或子层等的框架和边界,但都没有用。 CATransform3D、CATransform3DRotates 等也没有解决问题。我也用谷歌搜索了整整一个小时。要么没有人遇到您的问题,要么没有人解决您的问题。无论如何,这个解决方案是可行的,除非一些 Core Animation 大师可以提供更好的解决方案,否则欢迎您使用:
- (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"];
}
关于iOS:在 rootViewController 动画不正确的方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9842949/