代码之家  ›  专栏  ›  技术社区  ›  Moshe

手动淡入新添加的子视图?

  •  16
  • Moshe  · 技术社区  · 14 年前

    我希望视图在通过添加到堆栈时淡入

    [self.view addSubview:someSecondaryViewController.view];

    如何设置此调用的动画以使视图淡入(和淡出)?

    3 回复  |  直到 8 年前
        1
  •  29
  •   drawnonward    14 年前

    在设置动画之前将alpha设置为零,然后将alpha设置为1。

    [fadingView setAlpha:0.0];
    [containerView addSubview:fadingView];
    [UIView beginAnimations:nil context:nil];
    [fadingView setAlpha:1.0];
    [UIView commitAnimations];
    

    在移除视图之前,只需将alpha动画设置回零。

    顺便说一句,视图层次结构更像是一棵树,而不是一个堆栈。

    编辑:

    如果在淡出视图时动画结束后没有其他清理,请使用:

    [UIView setAnimationDelegate:fadingView];
    [UIView setAnimationDidStopSelector:@selector(removeFromSuperview)];
    

    如果已经设置了didstopselector,请在此处调用removefromsuperview。

        2
  •  21
  •   WebSeed    8 年前

    完成淡出动画后,还可以使用块从超级视图中删除视图:

    [UIView animateWithDuration:0.2
                     animations:^{viewOut.alpha = 0.0;}
                     completion:^(BOOL finished){[viewOut removeFromSuperview];}];
    
        3
  •  1
  •   dumbledad    8 年前

    在斯威夫特

    In

    someSecondaryViewController.view.alpha = 0.0
    self.view.addSubview(someSecondaryViewController.view)
    UIView.animateWithDuration(0.2, animations: { self.someSecondaryViewController.view.alpha = 1.0 })
    

    Out

    UIView.animateWithDuration(0.2, animations: { self.someSecondaryViewController.view.alpha = 0.0 }) { (done:Bool) in
            self.someSecondaryViewController.view.removeFromSuperview()
    }