2011-03-08 60 views
3

为了使用UISplitViewController,我在从一个视图控制器导航到另一个视图控制器时替换了我的窗口根控制器。UIViewController的动画设置错误方向

为了有一些很好的过渡,而这样做,我使用的是变焦效果是这样的:

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; 
    } 
}]; 

,这工作得很好,只是在做动画,新视图总是无论当前的设备方向如何,都可以在纵向模式下进行定向。当动画完成时,视图正确定向。

我错过了什么?

事情我已经尝试:

  • 把我的新控制器视图的一个UIWindow
  • 唯一的子视图使我的新控制器的根视图控制器动画开始

A之前好奇的是,如果我在我的方法开始处的窗口上做了递归描述,窗口框架被定义为具有768x1024(即,纵向)的尺寸,并且其内部的视图为748x1024,但是具有[ 0,-1,1,0,0,0](做这个mea旋转或什么?它应该不是身份转换吗?)

回答

2

我终于明白出了什么问题。由于框架不是一个真实的属性,而是一种基于视图边界和视图变换的计算值,我需要在设置与当前视图相同的变换之后设置框架,并且在再次设置变换之前设置动画的初始状态。此外,我需要设置的帧与当前视图当前使用的帧相同,因为它考虑了窗口方向(或者像Rob Napier指出的那样缺少其方向)

因此,没有更多的了解,这里是工作代码:

MyOtherViewController *controller = [[MyOtherViewController alloc] initWithNibName:@"MyOtherView" bundle:nil]; 
UIWindow *window = [[UIApplication sharedApplication] keyWindow]; 

CGAffineTransform t = self.view.transform; 
controller.view.transform = t; 
controller.view.frame = self.view.frame; 
controller.view.transform = CGAffineTransformScale(t,.01,.01);; 
[window addSubview:controller.view]; 

controller.view.alpha = 0; 

[UIView animateWithDuration:0.2 animations:^{ 
    controller.view.transform = t; 
    controller.view.alpha = 1.0; 
} completion:^(BOOL finished) { 
    if (finished) { 
     [self.view removeFromSuperview]; 
     window.rootViewController = controller; 
     [controller release]; 
    } 
}]; 
3

UIWindow不旋转。它有一个旋转的视图(如你所见)。不过,在这种情况下,我认为问题很可能是您的视图已经在此处进行了转换,您需要将它连接起来,而不是像在setTransform:调用中那样替换它。

你不应该问窗口的应用程序委托,你应该从视图中获取窗口(self.view.window)。

如果您在任何时候将视图附加到窗口本身,而不是将其放置在旋转视图中,则需要通过遍历层次结构来了解要匹配的视图的有效变换:

- (CGAffineTransform)effectiveTransform { 
    CGAffineTransform transform = [self transform]; 
    UIView *view = [self superview]; 
    while (view) { 
     transform = CGAffineTransformConcat(transform, [view transform]); 
     view = [view superview]; 
    } 
    return transform; 
} 
+0

是我有点使用与我当前视图中使用相同的转换。由于它们都是窗口的直接子视图,我想必须应用相同的变换才能获得相同的结果是合乎逻辑的。 不幸的是,既没有应用你提到的构图,也没有应用窗口变换,也没有应用视图变换。 – 2011-03-09 09:16:00

+0

我刚刚发布我的答案。由于它证实了我怀疑这个窗口没有旋转,而是应用了一个变换,所以我选择了这个。 – 2011-03-09 10:34:23