dismissViewControllerAnimated custom animation

2020-02-14 07:10发布

问题:

I am trying to call dismissViewControllerAnimated with a custom animation, but it does not seem to work.

When I present the view controller like:

        cameraUI = UIImagePickerController()
    cameraUI.delegate = self
    cameraUI.sourceType = UIImagePickerControllerSourceType.Camera
    cameraUI.mediaTypes = [kUTTypeImage]
    cameraUI.allowsEditing = false
    cameraUI.showsCameraControls = false

    var translate = CGAffineTransformMakeTranslation(0.0, 71.0); //This slots the preview exactly in the middle of the screen by moving it down 71 points
    cameraUI.cameraViewTransform = translate;

    var scale = CGAffineTransformScale(translate, 1.333333, 1.333333);
    cameraUI.cameraViewTransform = scale;


    var transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionMoveIn
    transition.subtype = kCATransitionFromLeft
    self.view.window?.layer.addAnimation(transition, forKey: nil)

    self.presentViewController(cameraUI, animated: false, completion: nil)

That works great. It slides in properly.

Then I go to dismiss it:

            var transition = CATransition()
        transition.duration = 0.5
        transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        transition.type = kCATransitionMoveIn
        transition.subtype = kCATransitionFromRight
        self.view.window?.layer.addAnimation(transition, forKey: nil)
        cameraUI.dismissViewControllerAnimated(false, completion: nil)

It just disappears without animating at all.

Any thoughts?

回答1:

The way you customize the animation that occurs when presenting or dismissing a view controller is not by supplying a transition behind it, but by customizing the actual animation. Give the presented view controller a transitioningDelegate that implements animationControllerForPresentedController:presentingController:sourceController: and animationControllerForDismissedController:. Now the animation controller is in total charge of the animation, through its implementation of animateTransition:.