Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a fade out animation while dismissing a view controller

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

like image 399
SriTeja Chilakamarri Avatar asked Oct 21 '25 11:10

SriTeja Chilakamarri


1 Answers

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.

like image 111
成文权 Avatar answered Oct 24 '25 00:10

成文权



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!