IOS:查看翻译动画。合适的方式。

时间:2013-12-07 01:47:26

标签: ios objective-c animation

我正在实现自己的导航控制器,我正在伪装添加新视图的动画,就像在UINavigationContoller中一样。

以下代码效果很好,但是如果我在UIView动画调用之前移动-addSubview:,它将在ios7中动画,但在ios6中不动画,问题是为什么? (我的意思是,为什么它会产生影响,因为动画将被安排并执行异步,对吧?或者我可能会认为它会以某种方式影响动画的起始状态?)

- (void)didAddViewControllerInNavigationStack:(NSArray*)navigationStack
{
    if (self.activeViewController) {
        [self.activeViewController viewWillDisappear:YES];
        [self.activeViewController viewDidDisappear:YES];
    }

    self.activeViewController = navigationStack.firstObject;
    if (!self.activeViewController) return;


    CGRect windowRect = [[UIScreen mainScreen] bounds];
    windowRect.origin.x = windowRect.size.width;
    self.activeViewController.view.frame = windowRect;


    [self.activeViewController viewWillAppear:YES];

    [UIView transitionWithView:self.view
                      duration:0.32
                       options:UIViewAnimationOptionCurveEaseOut
                    animations:^ { self.activeViewController.view.frame = [[UIScreen mainScreen] bounds]; }
                    completion:^(BOOL isDone){[self.activeViewController viewDidAppear:YES];}];

    [self.view addSubview:self.activeViewController.view];
    [UIView commitAnimations];


}

1 个答案:

答案 0 :(得分:1)

transitionWithView:duration:options:animations:completion:的文档中可以看出,您应该在动画块中添加子视图:

  

您在动画参数中指定的块包含您要进行的任何状态更改。您可以使用此块添加,删除,显示或隐藏指定视图的子视图。

此外,您在未调用commitAnimations的情况下呼叫beginAnimations:context:。你在这里混合东西。基于块的动画API不需要开始端API。

您的代码应为:

[UIView transitionWithView:self.view
                      duration:0.32
                       options:UIViewAnimationOptionCurveEaseOut
                    animations:^ { [self.view addSubview:self.activeViewController.view]; self.activeViewController.view.frame = [[UIScreen mainScreen] bounds]; }
                    completion:^(BOOL isDone){[self.activeViewController viewDidAppear:YES];}];