【问题标题】:Animated transition doesn't work correctly in landscape orientation动画过渡在横向上无法正常工作
【发布时间】:2011-01-16 19:39:40
【问题描述】:

我的应用只支持横向向右。

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

- (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 中设置了它,所以它都是横向布局的。

编辑:我还尝试将视图控制器添加到自己的 IB 文件并转换到该视图(认为初始化和转换之间可能太接近了)但同样的事情发生了。

【问题讨论】:

    标签: iphone objective-c animation uiview transition


    【解决方案1】:

    嗯,主要原因是 self.view(当前“知道”您处于横向)正在被一个没有该信息的新视图所取代。因此,一种想法是将 aViewController.view 作为 self.view 的子视图(假设 aViewController 是不透明的)。啊,你说,但后来我失去了transitionFromView的漂亮动画。好吧,试试这个好东西:

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

    如果由于某种原因对您不起作用,另一种方法是“教”aViewController.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 正在泄漏,因此您应该将其设为父 viewController 的保留属性。

    【讨论】:

    • 非常重要的答案,拯救了我的一天!
    • 也拯救了我的一天,但第二个选项中有一个小错字:[self setViewToLandscape:aViewController.view];应该是:[self setNewViewToLandscape:aViewController.view];使用您的命名约定。此外,如果您的新视图控制器处于横向状态,只需在此处交换宽度和高度: [viewObject setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)]; (这将使它正确居中。
    • 谢谢,我已经修正了错字;不知道那是怎么溜进来的。
    猜你喜欢
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 2014-11-29
    • 2016-02-05
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 2020-10-29
    相关资源
    最近更新 更多