如果一个动画是在另一个正在进行动画的视图的子视图上完成的,则您可能会遇到两个动画相互干扰的行为。因此,如果您执行transitionWithView:self.view
(即在主视图上)就像您的代码片段所示,您可能会遇到问题。如果您在不同的子视图上执行两个动画,问题可能会消失。在我下面的原始答案中,我:
当我将两个动画部分放在不同的子视图上时,动画可以同时发生而不会发生意外。
如果您想以动画方式更改两个文本标签的内容,您可以:
[UIView transitionWithView:self.viewController.textLabelsContainerView
duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.viewController.productTitleLabel.text = [_flavoredOliveOilsTableView cellForRowAtIndexPath:indexPath].textLabel.text;
self.viewController.productDescriptionTextView.text = [selectedCellDict objectForKey:@"Description"];
}
completion:nil];
一般来说我会用animateWithDuration
,但是文本属性不是可动画属性,所以这就是我使用的原因transitionWithView
,确保我有一个用于这些文本字段的容器。但我尝试过使用其他控件的动画animateWithDuration
,同时对子控制器的变化进行动画处理,并且效果很好。
为了转换子视图控制器,我使用transitionFromViewController
为容器子控制器视图的交换设置动画(它是管理自定义容器中的子视图控制器方法族)。为了促进该过程,我在主视图控制器的视图上放置了一个容器视图,并将子控制器的视图添加为该容器的子视图。这样,当我为子级的过渡设置动画时,动画就很好地限制在该容器视图中。
因此,这里是一些示例代码,用于将视图控制器添加到我的容器视图,然后是我使用分段按钮在两个子视图控制器之间进行转换的代码:
- (void)addFirstChild
{
UIViewController *child = [self.storyboard instantiateViewControllerWithIdentifier:@"Stooge"];
[self addChildViewController:child];
child.view.frame = self.bottomContainerView.bounds;
[self.bottomContainerView addSubview:child.view];
[child didMoveToParentViewController:self];
}
- (void)changedChild
{
UIViewController *oldController = [self.childViewControllers lastObject];
UIViewController *newController;
if (self.segmentedControl.selectedSegmentIndex == 0)
newController = [self.storyboard instantiateViewControllerWithIdentifier:@"Stooge"];
else
newController = [self.storyboard instantiateViewControllerWithIdentifier:@"Marx"];
[oldController willMoveToParentViewController:nil];
[self addChildViewController:newController];
// start off screen below
CGRect startFrame = self.bottomContainerView.bounds;
startFrame.origin.y += startFrame.size.height;
// end up where it's supposed to be, right in the container
newController.view.frame = startFrame;
[self transitionFromViewController:oldController
toViewController:newController
duration:0.5
options:0
animations:^{
newController.view.frame = self.bottomContainerView.bounds;
}
completion:^(BOOL finished) {
[oldController removeFromParentViewController];
[newController didMoveToParentViewController:self];
}];
}
最重要的是,我对容器子控制器和父控制器中的字段进行动画处理没有问题。也许我不明白你所描述的问题。或者也许我们的动画制作方式存在一些微妙的差异。如果你想要的话,我已经放了上面的代码如果你想看的话可以在 github 上查看。