18

Quiero agregar una subvista con animación. Estoy utilizando añadir sub vista por lo que no está mostrando ningún tipo de animación, así que quiero mostrar cualquier animación cuando estoy haciendo esto ... Estoy utilizando a continuación código: -mostrar animación cuando 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; 

Puede alguno sugerir cómo hacer esto ?

Respuesta

38

Aquí está el código ... Pruébalo.

PD: Reemplace myView con el nombre de la vista que desea reemplazar.

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

aquí es para los bloques de animación

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

Y para eliminar la subvista? – djskinner

+1

Para eliminar la subvista, use el mismo código, pero reemplace [containerView addSubview: subview]; con [self.view removeFromSuperview]; – rmooney

0

Tal vez usted puede subclase el método UIView y anulación willMove(toSuperview newSuperview: UIView?)

Aquí es ejemplo:

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