设置UIViewController动画时的方向错误 [英] wrong orientation on UIViewController while animating it

查看:61
本文介绍了设置UIViewController动画时的方向错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了使用UISplitViewController,在从一个视图控制器导航到另一个视图控制器时,我要替换我的窗口根控制器。

In order to use a UISplitViewController, I'm replacing my window root controller when navigating from one view controller to the other.

为了进行一些不错的过渡这样做时,我使用的是这样的缩放效果:

In order to have some nice transition while doing so, I'm using a zooming effect like this:

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

效果很好,除了在制作动画时,新视图始终像在纵向模式下,无论当前设备的方向如何。动画结束后,视图将正确定向。

and this works pretty well, except that while doing the animation, the new view is always oriented as if in portrait mode, regardless of the current device orientation. When the animation is finished, the view orients itself correctly.

我缺少了什么?

我要注意的事情ve尝试过:

Things I've tried:


  • 将新的控制器视图作为UIWindow的唯一子视图

  • 新控制器动画开始之前的根视图控制器

奇怪的是,如果我在开始时在窗口上执行recursiveDescription按照我的方法,窗框被定义为尺寸为768x1024(即,纵向),并且其内部视图为748x1024,但变换为[0,-1,1,0,0,0](这样做

A curious thing is that, if I do a recursiveDescription on the window at the beginning of my method, the window frame is defined as having a size of 768x1024 (i.e., portrait), and the view inside it of 748x1024 but with a transform of [0, -1, 1, 0, 0, 0] (does this mean a rotation or what? Shouldn't it be the identity transform?)

推荐答案

我终于弄清楚了什么地方出了问题。由于框架不是真正的属性,而是一种经过计算的属性,因此,基于视图范围和视图变换的值,我需要在设置与当前视图相同的变换之后并再次设置变换之前设置框架设置动画的初始状态。此外,我需要设置的框架与当前视图当前使用的框架相同,因为它考虑了窗口方向(如Rob Napier所指出的那样缺少窗口方向)

I finally figured out what was wrong. Since the frame is not a real property but a sort of calculated one, based on the values of the view bounds and the view transform, I needed to set the frame after setting the same transform as the current view, and before setting the transform again to set up the initial state of the animation. Also, the frame I need to set is the same one as the current view is currently using, as it is taking into account the window orientation (or lack thereof, as Rob Napier pointed)

因此,事不宜迟,这是工作代码:

So, without further ado, here's the working code:

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

这篇关于设置UIViewController动画时的方向错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆