5

In Objective-C, I want to dismiss modal ViewController from right to left.

Now I can move from left to right on modal. But I don't know how to dismiss this modal VC from left to right like push screen transition.

Here is a code which moves from a-ViewController to b-ViewController.

bViewController *nextVC = [[bViewController alloc] initWithNibName:nil bundle:nil];

CATransition *transition = [CATransition animation];
transition.duration = 0.5; 
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
transition.type = kCATransitionPush ; 
[transition setSubtype:kCATransitionFromLeft];
[[nextVC.view layer] addAnimation:transition forKey:@"TransitionTest"]; 
//[self presentViewController:nextVC animated:YES completion:nil];
//if it's possible, I want to use upside's cord.(don't addSubView) but I don't know how to do it)
[self.view addSubview:nextVC.view] ;
0

5 Answers 5

14

Objective-C

CATransition *transition = [CATransition animation];
transition.duration = 0.4;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[self.view.window.layer addAnimation:transition forKey:nil];
[self dismissViewControllerAnimated:NO completion:nil]; 

Swift 5

let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
transition.type = CATransitionType.push
transition.subtype = CATransitionSubtype.fromRight
self.view.window!.layer.add(transition, forKey: nil)
self.dismiss(animated: false, completion: nil)
1
  • Thank you for answering.
    – K.K.D
    Feb 29, 2016 at 8:52
5

In Swift 4, You may use Bellow code. It is Working for me..

        let transition = CATransition()
        transition.duration = 0.5
        transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        transition.type = kCATransitionPush
        transition.subtype = kCATransitionFromRight 
        self.view.window!.layer.add(transition, forKey: nil)
        self.dismiss(animated: false, completion: nil)
2
    -(void) dismiss {
        CATransition *transition = [CATransition animation];
        transition.duration = 0.5;
        transition.timingFunction =
        [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        transition.type = kCATransitionMoveIn;
        transition.subtype = kCATransitionFromRight;

        UIView *containerView = self.view.window;
        [containerView.layer addAnimation:transition forKey:nil];

        [self dismissModalViewControllerAnimated:NO]; 
    }
1
  • Thank you for answering.
    – K.K.D
    Feb 29, 2016 at 8:52
2

In Swift 3

let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
view.layer.add(transition, forKey: "rightToLeftTransition")
dismiss(animated: true, completion: nil)
1

Define CATransition and implement the above implementation. This code can make screen transitions to the left and right using dismiss. The point is to make animated false. If you set animated to true, the original movement of dismiss is confused.

    transition.duration = 0.5
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromLeft
    view.window!.layer.add(transition, forKey: kCATransition)
    dismiss(animated: false, completion: nil)
3
  • Please edit your answer instead of adding more lines of code in a comment.
    – yacc
    Nov 19, 2017 at 1:32
  • To improve the quality of your answer, please add an explanation of how your code improves theirs.
    – toonice
    Nov 19, 2017 at 3:53
  • Define CATransition and implement the above implementation. This code can make screen transitions to the left and right using dismiss. The point is to make animated false. If you set animated to true, the original movement of dismiss is confused. Nov 19, 2017 at 4:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.