动画过渡在横向方向上无法正常工作 [英] Animated transition doesn't work correctly in landscape orientation

查看:112
本文介绍了动画过渡在横向方向上无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序仅支持横向右方向。

My app only supports the landscape right orientation.

我正在尝试转换到视图控制器的视图,但是当它执行过渡动画时(例如curl) up),视图旋转90度(因此,模拟器仍处于横向右方向,但视图显示旋转90度)。转换完成后,它将旋转到正确的方向。我的代码如下。

I'm trying to transition into a view controller's view, but when it performs the transition animation (such as curl up), the view is rotated 90 degrees (so, the simulator is still in landscape-right orientation, but the view appears rotated by 90 degrees). When the transition finishes it rotates to the correct orientation. My code is below.

- (IBAction)buttonTouched
{
    MyViewController *aViewController = [[MyViewController alloc] initWithNibName:NSStringFromClass([MyViewController class]) bundle:nil];

    [UIView transitionFromView:self.view 
                        toView:aViewController.view 
                      duration:2 
                       options:UIViewAnimationOptionTransitionCurlUp 
                    completion:NULL];
}

好像视图没有获得方向已更改通知,因此它以纵向呈现,然后在动画结束后更改为横向。我已经在IB中进行了设置,所以它都是横向布局的。

It appears as though the view doesn't get the "orientation changed" notification, so it presents it in portrait, and then changes to landscape after the animation finishes. I have set it up in IB so that it is all laid out in landscape.

编辑:我还尝试将视图控制器添加到自己的IB文件并转换到该视图(想想也许它在初始化和转换之间过于接近)但同样的事情发生了。

I also tried adding a View Controller to self's IB file and transitioning to that view (thinking maybe it was too close between the initialization and the transition) but the same thing happened.

推荐答案

嗯,主要原因是self.view(目前知道你在景观中)被替换为没有该信息的新视图。因此,我们只想将aViewController.view作为self.view的子视图(假设aViewController是不透明的)。啊,你说,但后来我失去了transitionFromView的漂亮动画。好吧,试试这个好处:

Well, the main reason is that self.view (which currently "knows" that you're in landscape) is being replaced with a new view which doesn't have that information. So one thought is to just put aViewController.view as a subView of self.view (assuming that aViewController is opaque). Ah, you say, but then I lose the nice animation of transitionFromView. Well, try this niceness:

[UIView transitionWithView:self.view
                  duration:2 
                   options:UIViewAnimationOptionTransitionCurlUp
                animations:^{
                    [self.view addSubview:aViewController.view];
                } 
                completion:NULL];

如果出于某些原因对您不起作用,另一种方法是教aViewController。查看它实际上是一个横向视图:

If for some reason that doesn't work for you, the alternative is to "teach" aViewController.view that it's really a landscape view:

#define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI)
-(void)setNewViewToLandscape:(UIView*)viewObject {
     //assumes self.view is landscape and viewObject is portrait
     [viewObject setCenter:CGPointMake( self.view.frame.size.width/2,self.view.frame.size.height/2)];
     CGAffineTransform cgCTM = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(90));
     viewObject.transform = cgCTM;
     viewObject.bounds = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
}

 MyViewController *aViewController = [[MyViewController alloc] initWithNibName:NSStringFromClass([MyViewController class]) bundle:nil];
[self setNewViewToLandscape:aViewController.view];

[UIView transitionFromView:self.view 
                    toView:aViewController.view 
                  duration:2 
                   options:UIViewAnimationOptionTransitionCurlUp 
                completion:NULL];

一个小的额外点是你的aViewController泄漏,所以你应该把它作为一个保留的属性parent viewController。

One minor additional point is that your aViewController is leaking, so you should make it a retained property of the parent viewController.

这篇关于动画过渡在横向方向上无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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