为了使用 UISplitViewController,我在从一个 View Controller 导航到另一个 View Controller 时替换了我的窗口根 Controller 。
为了在这样做时有一些漂亮的过渡,我使用了这样的缩放效果:
MyOtherViewController *controller = [[MyOtherViewController alloc] initWithNibName:@"MyOtherView" bundle:nil];
UIWindow *window = ((MyAppDelegate *)[[UIApplication sharedApplication] delegate]).window;
controller.view.frame = [window frame];
controller.view.transform = CGAffineTransformMakeScale(0.01,0.01);
controller.view.alpha = 0;
[window addSubview:controller.view];
[UIView animateWithDuration:0.2 animations:^{
controller.view.transform = CGAffineTransformMakeScale(1,1);
controller.view.alpha = 1.0;
} completion:^(BOOL finished) {
if (finished) {
[self.view removeFromSuperview];
window.rootViewController = controller;
}
}];
这工作得很好,除了在做动画时,新 View 总是像纵向模式一样定向,而不管当前的设备方向如何。动画结束后, View 会正确定位自身。
我错过了什么?
我尝试过的事情:
- 将我的新 Controller View 作为 UIWindow 的唯一 subview
- 在动画开始前让我的新 Controller 成为 Root View Controller
奇怪的是,如果我在我的方法开始时对窗口执行递归描述,窗口框架被定义为大小为 768x1024(即纵向),其中的 View 为 748x1024 但[0, -1, 1, 0, 0, 0] 的变换(这是旋转还是什么意思?不应该是恒等变换吗?)
最佳答案
UIWindow
不旋转。它内部有一个旋转 View (如您所见)。不过,在这种情况下,我认为问题很可能是您的 View 此时已经对其进行了转换,您需要将其连接起来而不是像在 setTransform:中那样替换它:
调用。
您不应该向应用委托(delegate)询问窗口,您应该从 View (self.view.window
) 获取窗口。
如果在任何时候您将 View 附加到窗口本身,而不是将其放在旋转 View 中,那么您需要通过遍历层次结构来了解要匹配的 View 的有效变换:
- (CGAffineTransform)effectiveTransform {
CGAffineTransform transform = [self transform];
UIView *view = [self superview];
while (view) {
transform = CGAffineTransformConcat(transform, [view transform]);
view = [view superview];
}
return transform;
}
关于ios - 动画时 UIViewController 的方向错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5234182/