iOs Segue animation left to right (horizontal)

22,778

Solution 1

You can use CATransition within a custom Segue to achieve left to right transition. Add this #import "QuartzCore/QuartzCore.h" to your custom Segue

-(void)perform {

__block UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
__block UIViewController *destinationController = (UIViewController*)[self destinationViewController];                    

CATransition* transition = [CATransition animation];
transition.duration = .25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom



[sourceViewController.navigationController.view.layer addAnimation:transition
                                            forKey:kCATransition];

[sourceViewController.navigationController pushViewController:destinationController animated:NO];    


}

Solution 2

Based on Zakir Hyder's submission, here is the same functionality translated to Swift:

override func perform() {

    var sourceViewController = self.sourceViewController as UIViewController
    var destinationViewController = self.destinationViewController as UIViewController

    var transition: CATransition = CATransition()

    transition.duration = 0.25
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
    transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom

    sourceViewController.navigationController?.view.layer.addAnimation(transition, forKey: "kCATransition")
    sourceViewController.navigationController?.pushViewController(destinationViewController, animated: false)     

}

Solution 3

Here is the working code for custom seque when not using navigation controller.

-(void)perform
{

UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
UIViewController *destinationController = (UIViewController*)[self destinationViewController];

CATransition* transition = [CATransition animation];
transition.duration = 0.25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromRight; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom

[destinationController.view.layer addAnimation:transition forKey:kCATransition];
[sourceViewController presentViewController:destinationController animated:NO completion:nil];
}

Solution 4

Check out this Example on Github:

https://github.com/itinance/iOSCustomSegue

It uses a custom segue from right to left in an example app explicitly without UINavigationController as the thread opener where asking for.

The Code in the answer of Sahil didn't work for me at iOS 8. So i had to investigate a little bit more on this issue.

'UIView animateWithDuration' works on iOS 8, while 'destinationController.view.layer addAnimation:transition' does nothing in combination with presentViewController.

Solution 5

I tried using Segue to do the same effect too, but without success. Instead, I used a workaround:

// get the view that's currently showing
UIView *currentView = self.view;
// get the the underlying UIWindow, or the view containing the current view
UIView *theWindow = [currentView superview];

UIView *newView = aTwoViewController.view; 

// remove the current view and replace with myView1
[currentView removeFromSuperview];
[theWindow addSubview:newView];

// set up an animation for the transition between the views
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

[[theWindow layer] addAnimation:animation forKey:@"SwitchToView2"];

You can download the sample project here. Cheers!

Share:
22,778
Sasha Grievus
Author by

Sasha Grievus

Updated on July 09, 2022

Comments

  • Sasha Grievus
    Sasha Grievus almost 2 years

    I'm nearly a newbie to Xcode 4. There's a way to add to a segue a custom transition animation that it is not among the four presented by the Interface Builder in the storyboard management? Specifically, i want an animation similar to the normal "cover vertical", but horizontal. I want the a view to pass to another sliding from left to right (or right to left) instead that from up to bottom as it happens in the "cover vertical" transition. I tried with the swipe gesture but no fortune: even that is transitioning from up to bottom and anyway, i don't understand why the defaul transition is from up to bottom when the default transition of all the app usually is right to left or left to right, especially in the case you do swipe...

    I tried also a programatically way but no fortune even in this case, using this code:

    #import "JHCustomSegue.h"
    
    @implementation JHCustomSegue
    - (void) perform {
      UIViewController *src = (UIViewController *) self.sourceViewController;
      UIViewController *dst = (UIViewController *) self.destinationViewController;
    
      [UIView transitionWithView:src.navigationController.view duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
            [src presentModalViewController:dst animated:NO];
        }
        completion:NULL];
    }
    
    @end
    

    In the interface builder i defined this class as the class of my segue. Using breakpoint, i saw it enter the function perfom but... don't perform! I've blocked the app in portrait mode (don't know if it's a problem). I tried to run the application on a real ipad and on a simulated iphone. Same Problem.

  • justinkoh
    justinkoh about 12 years
    Something I missed to add was if you did found a way to do it with Segue, do let us know. =)
  • Sasha Grievus
    Sasha Grievus about 12 years
    no, until now :) I think, however, that it is possible working more with navigation controllers. As soon as i can, i will dedicate some time to this.
  • emil.c
    emil.c over 10 years
    Is it possible that the screen pushes up from bottom? I used this very code as custom segue and this does exactly what I mentioned.
  • emil.c
    emil.c over 10 years
    I found out where's the problem. It's landscape and it acts as if it was portrait.
  • Zakir Hyder
    Zakir Hyder over 10 years
    you can use kCATransitionFromBottom for transition.subtype. see here developer.apple.com/library/ios/documentation/GraphicsImagin‌​g/…
  • emil.c
    emil.c over 10 years
    Yes, that's exactly what I did. Anyway Jakir, thanks for a great answer above, really helpful.
  • donkey
    donkey almost 9 years
    this is essentially the same answer as @Zakir Hyder but in a different form.
  • A. Vin
    A. Vin about 8 years
    What would this look like in Swift?
  • A. Vin
    A. Vin about 8 years
    What would this code look like in Swift if not using a Navigation Controller?