dismissViewControllerAnimated 自定义动画

Posted

技术标签:

【中文标题】dismissViewControllerAnimated 自定义动画【英文标题】:dismissViewControllerAnimated custom animation 【发布时间】:2014-11-09 23:07:39 【问题描述】:

我正在尝试使用自定义动画调用dismissViewControllerAnimated,但它似乎不起作用。

当我呈现视图控制器时:

        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)

效果很好。它可以正确滑入。

然后我去解散它:

            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)

它完全没有动画就消失了。

有什么想法吗?

【问题讨论】:

【参考方案1】:

您自定义呈现或关闭视图控制器时发生的动画的方式不是通过在其后面提供过渡,而是通过自定义实际动画。给呈现的视图控制器一个 transitioningDelegate 实现 animationControllerForPresentedController:presentingController:sourceController:animationControllerForDismissedController:。现在动画控制器通过 animateTransition: 的实现完全负责动画。

【讨论】:

有关实际示例,请在此处查看我的可下载项目:github.com/mattneub/custom-alert-view-ios7 请注意,我们在演示时获得了一个自定义动画,而在解雇时获得了不同的自定义动画。

以上是关于dismissViewControllerAnimated 自定义动画的主要内容,如果未能解决你的问题,请参考以下文章

dismissViewControllerAnimated:completion: 导致内存崩溃

在当前动画完成之前开始dismissViewControllerAnimated

调用了dismissViewControllerAnimated,但没有解除ViewController

状态恢复后,dismissViewControllerAnimated 没有使用相同的动画

dismissViewControllerAnimated() 不会关闭视图控制器

dismissViewControllerAnimated:completion: 方法替换在 Xcode4.6.1 上不起作用