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