同时动画两个UIView子视图是行不通的



由于某些原因,我不能两个动画两个子视图的位置。我写了如下

[self addChildViewController:self.photosViewController];
[self.photosViewController.view setFrame:CGRectMake(0, -self.view.frame.size.height,    self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.photosViewController.view];
[UIView animateWithDuration:1 delay:0
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         [self.stepsView setFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
                         [self.photosViewController.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                     }
                     completion:^(BOOL finished) {
                         if (finished) {
                             button.tag = 0;
                         }
                     }];

self.stepsViewIBOutletUIViewself.photosViewController.view为已添加到视图中的子视图控制器视图。

当前代码只有self.PhotosViewController.view动画。但是,如果我注释掉我将子视图控制器视图添加为subview的行,那么self.stepsView就会正确地动画。

即使我在调用这个方法之前添加了子视图控制器和它的视图,也会发生同样的错误。

需要帮助,因为我在几个月前用另一个应用程序遇到了这个问题,不得不做一个肮脏的hack来绕过它,现在想解决这个问题。

谢谢

在视图编程指南中的视图动画部分中,有一个关于如何为子视图制作动画的特别提到。

基本上,您应该使用transitionWithView:duration:options:animations:completion:而不是animateWithDuration:delay:options:animations:completion:

第一个视图将不可见,因为它的起源。Y超出了可见视图的高度。如果它在动画开始时不可见,那么您当然将什么也看不到。

第二个视图被更改为填充屏幕。同样,您看到的内容取决于它的初始位置。

最新更新