18

我想添加动画的子视图。我使用添加子视图,它没有显示任何动画,所以我想表明,当我这样做的任何动画...我使用下面的代码: -显示动画时addsubview

UIViewController *vControllerHome = [[viewTemp alloc] initWithNibName:@"viewTemp" bundle:nil]; 
vControllerHome.view.frame =CGRectMake(0, 0, 320, 414); 
[self.view addSubview:vControllerHome.view]; 
self.selectedViewController = vControllerHome; 

任何一个可以建议我怎么做?

回答

38

下面是代码..只需尝试一下。

PS:用您要替换的视图的名称替换myView

CATransition *applicationLoadViewIn =[CATransition animation]; 
[applicationLoadViewIn setDuration:duration]; 
[applicationLoadViewIn setType:kCATransitionReveal]; 
[applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]; 
[[myView layer]addAnimation:applicationLoadViewIn forKey:kCATransitionReveal]; 
26

这里是动画块

[UIView transitionWithView:containerView 
        duration:0.5 
       options:UIViewAnimationTransitionFlipFromRight //any animation 
      animations:^ { [containerView addSubview:subview]; } 
      completion:nil]; 
+2

并删除子视图? – djskinner 2012-08-15 11:47:26

+1

要删除子视图,请使用相同的代码,但替换[containerView addSubview:subview];与[self.view removeFromSuperview]; – rmooney 2014-05-13 19:50:05

0

也许你可以子类的UIView和重写方法willMove(toSuperview newSuperview: UIView?)

这里是例子:

override public func willMove(toSuperview newSuperview: UIView?) { 
    super.willMove(toSuperview: newSuperview) 

    if let _ = newSuperview { 
     // This view will be added to some view 
     UIView.animate(withDuration: 0.2, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 30.0, options: .curveEaseInOut, animations: { 
      //... 
     }, completion: { (finish) in 

     }) 
    } else { 
     // This view will be removed 
    } 
}