Add Slide In from Left to Right animation(transition) in addSubView
Asked Answered
L

2

17

I Try to imitate the NavigationViewController, very similar to default MailApp in iPhone

When clicking on a Mail summary, it should slide-in the mail details, and when BACK button is clicked, Mail Summary view is to be slided-in back again.

This is what I have to animate the transition from Summary To Detail (removeFromSuperView)

CGRect temp = self.view.frame;
temp.origin.x = 300;
[UIView animateWithDuration:0.5
                      delay:0.0
                    options: UIViewAnimationCurveEaseOut
                 animations:^{
                     self.view.frame = temp;
                 }completion:^(BOOL finished){
                     [self.view removeFromSuperview];
                 }];

This is what I have to animate the transition from Detail To Summary (addSubview)

    CATransition *transition = [CATransition animation];
    transition.duration = 0.5;
    transition.type = kCATransitionFromRight;
    transition.subtype = kCATransitionFade;
    [parentView.layer addAnimation:transition forKey:nil];
    [parentView addSubview:myVC.view];

Now, that My First part of code is working well! Where-as the second part, I am just able to achieve the Fade-in animation. How can I bring in the slide transition?

Lubber answered 27/2, 2014 at 19:48 Comment(0)
L
64

I just need to choose kCATransitionPush type for my CATransition

    CATransition *transition = [CATransition animation];
    transition.duration = 0.5;
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;
    [transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [parentView.layer addAnimation:transition forKey:nil];

    [parentView addSubview:myVC.view];

Update for Swift:

    let transition = CATransition()
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromLeft
    parentView.layer.add(transition, forKey: nil)
    parentView.addSubview(myVC.view)
Lubber answered 27/2, 2014 at 20:34 Comment(3)
And what if I want to remove myVC.view from super view.Gentlewoman
@Gentlewoman You can add this line to remove the myVC.view: [myVC.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.5];Piquet
@Gentlewoman just change subtype into CATransitionSubtype.fromRightSausauce
M
0

Or you could check out : HorizontalSlide Segue (just google it I cant get the link, on mobile) its a great sliding transition that is simply a segue that you add from view A to B....

On a side note, if this is in a nav controller, a push or a pop should have a slide animation by default...

Methodist answered 27/2, 2014 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.