【问题标题】:Replace subview with another, animating size change用另一个动画大小更改替换子视图
【发布时间】:2015-05-20 08:53:14
【问题描述】:

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

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}];
                 }];

旧的内容不透明度动画正确,但布局更改时没有任何动画。我该如何解决?

【问题讨论】:

    标签: ios objective-c autolayout uikit


    【解决方案1】:

    //3 个视图(container,first,second) 在 vi​​ewdidload 中分配

    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) {
    
        }];
    }];
    

    我想你正在尝试同样的场景。它在我的情况下完美运行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多