将子视图替换为另一个,动画大小更改

时间:2015-05-20 08:53:14

标签: ios objective-c autolayout uikit

我有一个容器视图,其中包含一个子视图。我想用另一个与原始视图不同的视图替换此子视图,因此也应该调整容器的大小。这是我正在尝试做的事情

UIView *content = NEW_CONTENT;
content.translatesAutoresizingMaskIntoConstraints = NO;

[self.contentContainer layoutIfNeeded]; // the container view

// make the old content disappear first
[UIView animateWithDuration:0.3
                 animations:^{
                     UIView *oldContent = [self.contentContainer.subviews firstObject];
                     oldContent.alpha = 0.0f;
                 }
                 completion:^(BOOL finished) {
                     // then replace it with a new content
                     [UIView transitionWithView:self.contentContainer
                                       duration:0.1
                                        options:0
                                     animations:^{
                                         [[self.contentContainer.subviews firstObject] removeFromSuperview];
                                         [self.contentContainer addSubview:content];
                                         [content autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];

                                         [self.contentContainer layoutIfNeeded];
                                     }
                                     completion:^(BOOL finished) { // completion stuff}];
                 }];

旧内容不透明度正确动画,但布局在没有任何动画的情况下更改。我怎么能解决它?

1 个答案:

答案 0 :(得分:0)

//在viewdidload中分配3个视图(容器,第一个,第二个)

UIView *container=[[UIView alloc] initWithFrame:CGRectMake(20, 20, 250, 300)];
container.backgroundColor=[UIColor redColor];
[self.view addSubview: container];

UIView *first=[[UIView alloc] initWithFrame:CGRectMake(100, 50, 100, 100)];
first.backgroundColor=[UIColor blueColor];
[self.view addSubview: first];

UIView *second=[[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 150)];
second.backgroundColor=[UIColor purpleColor];
second.alpha=0;
[self.view addSubview: second];

//移动第一个视图的动画功能,更改容器框架并添加第二个视图

[UIView transitionWithView:first duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    first.alpha=0.0;

} completion:^(BOOL finished) {
    [UIView transitionWithView:second duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        container.frame=CGRectMake(50, 10, 100, 200);
        second.alpha=1.0;

    } completion:^(BOOL finished) {

    }];
}];

我想你应该尝试同样的场景。在我的情况下,它完美无缺