4

I have an Image view in my View Controller. I want to dismiss the view controller when a Pan gesture is recognized towards the down side of the view controller. While I have achieved dismiss the view controller with a Pan gesture, I am trying to figure out how can I add an animation before dismissing just like as in the Photos application in the iPhone.

 -(void)addPanGesture{

    UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dismissViewGes:)];
    gestureRecognizer.delegate = self;
    [self.view addGestureRecognizer:gestureRecognizer];


}



-(void)dismissViewGes:(UIPanGestureRecognizer *)gesture {


    CGPoint velocity = [gesture velocityInView:self.view];


    if(velocity.y > 0)
    {



        CATransition *transition = [[CATransition alloc]init];
        transition.duration = 2;
        [transition setValue:[NSNumber numberWithFloat:0.5f] forKey:kCATransitionFade];
        transition.subtype = kCATransitionFromTop;
        [self.view.layer addAnimation:transition forKey:nil];

        [[NetWrapper shared] addRemovedController:self];
        [self dismissViewControllerAnimated:NO completion:nil];


    }

}

I tried adding a CATransition hoping that the fade out animation will work but it is not working, the view dismisses as soon as the pan gesture is applied.

How can i add in a fade out while with the view dismissing just like in the photos application.

Photo viewer. I have this right now and when i use Pan gesture on this view it gets dismissed. But i want to add an animation so that it gets dismissed as in the second picture

This is the animation I want to achieve

Zee
  • 1,865
  • 21
  • 42
SriTeja Chilakamarri
  • 2,463
  • 4
  • 16
  • 29
  • you can take a view in front of main view then set background color as white with alpha as desired – Jitendra Modi Nov 30 '17 at 11:09
  • I tried it, even thats not working . `[UIView animateWithDuration:2.0f animations:^{ [self.view setAlpha:0.5]; } completion:^(BOOL finished) { [UIView animateWithDuration:0.3f animations:^{ [self.view setAlpha:0.1]; } completion:^(BOOL finished) { //[[NetWrapper shared] addRemovedController:self]; [self dismissViewControllerAnimated:NO completion:nil]; }]; }];` – SriTeja Chilakamarri Nov 30 '17 at 11:23
  • you forgot one line [self layoutifneeded]. Just write it before completion part – Jitendra Modi Nov 30 '17 at 11:31

3 Answers3

7

Try this:

@IBAction func clickDismiss(_ sender: Any) {
    let transition: CATransition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionFade
    transition.subtype = kCATransitionFromBottom
    self.view.window!.layer.add(transition, forKey: nil)
    self.dismiss(animated: false, completion: nil)
}

I present a SubViewController and add a UIButton on it. Add those codes to The button click event and It's work for me.

成文权
  • 87
  • 3
  • 1
    This works (But same as in my above code mentioned). What i actually wanted is that i want to show this like how apple shows animation in their photos app, like we are just throwing the view controller into the previous view. – SriTeja Chilakamarri Nov 30 '17 at 12:30
  • I found this tutorial you may refer to: [follow it](https://www.appcoda.com/custom-view-controller-transitions-tutorial/) – 成文权 Nov 30 '17 at 13:17
5

Swift 4.2, iOS 11

Here is an elegant way using extensions:

extension CATransition {
    func fadeTransition() -> CATransition {
        let transition = CATransition()
        transition.duration = 0.4
        transition.type = CATransitionType.fade
        transition.subtype = CATransitionSubtype.fromRight

        return transition
    }
}

Then you can just write these three lines of code in a function or inside the action button in your UIViewController:

private func dismissVC() {
    let transition = CATransition().fadeTransition()
    navigationController?.view.layer.add(transition, forKey: kCATransition)
    navigationController?.popViewController(animated: false)
}
Đorđe Nilović
  • 3,600
  • 1
  • 25
  • 20
  • 2
    This works great! I added an argument for the fadeTransition function that takes a double. Then pass the argument to the transition.duration. Then you can change the duration anywhere! – pcnick13 Feb 28 '19 at 23:57
0

If you need to add animation on presenting or dismissing a ViewController you should use UIViewControllerAnimatedTransitioning, there's a similar answer here: How do I do a Fade/No transition between view controllers

If you need it to be interactive, you can use UIPercentDrivenInteractiveTransition, which is a class that controls the transition in percentages.

About all this there's a great sample code from Apple: https://developer.apple.com/library/content/samplecode/CustomTransitions/Introduction/Intro.html It's a little old but it's simple.

RodolfoAntonici
  • 1,555
  • 21
  • 34