【问题标题】:IOS: View Translation Animation. Proper way.IOS:查看翻译动画。正确的方式。
【发布时间】:2013-12-07 01:47:25
【问题描述】:

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

下面的代码效果很好,但是如果我移动-addSubview:,在 UIView 动画调用之前,它会在 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];


}

【问题讨论】:

    标签: ios objective-c animation


    【解决方案1】:

    transitionWithView:duration:options:animations:completion:的文档中,据说你应该在动画块内添加子视图:

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

    另外,您在没有调用beginAnimations:context: 的情况下调用commitAnimations。你在这里混合东西。基于块的动画 API 不需要 begin-end 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];}];
    

    【讨论】:

    • 谢谢,很高兴知道,我刚开始玩动画,还有1个小问题,说如果我想做相反的事情,那么-removeFromSuperview:应该出现在完成块中吗?
    • 是的,因为您只想在动画完成后删除子视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多