18

I want to add a subview with animation. I am using add sub view so it is not showing any animation so I want to show any animation when I am doing this... I am using below code :-

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

Can any one suggest how I do this?

warren
  • 32,620
  • 21
  • 85
  • 124
Mitesh Khatri
  • 3,935
  • 4
  • 44
  • 67

3 Answers3

38

Here's the code.. Just try it.

PS: Replace myView with the name of the view you want to replace.

CATransition *applicationLoadViewIn =[CATransition animation];
[applicationLoadViewIn setDuration:duration];
[applicationLoadViewIn setType:kCATransitionReveal];
[applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[[myView layer]addAnimation:applicationLoadViewIn forKey:kCATransitionReveal];
Suresh Varma
  • 9,750
  • 1
  • 60
  • 91
27

here is for animation blocks

[UIView transitionWithView:containerView 
                  duration:0.5 
               options:UIViewAnimationTransitionFlipFromRight //any animation
            animations:^ { [containerView addSubview:subview]; }
            completion:nil];
adedoy
  • 2,275
  • 1
  • 20
  • 28
  • 1
    To remove the subview, use the same code, but replace [containerView addSubview:subview]; with [self.view removeFromSuperview]; – rmooney May 13 '14 at 19:50
0

Maybe you can subclass the UIView and override method willMove(toSuperview newSuperview: UIView?)

Here is example:

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
    }
}
Hawe
  • 11
  • 2