UIViewController交互式过渡-取消交互式关闭时,显示的视图消失

时间:2019-01-09 18:58:04

标签: ios swift uikit uikit-transitions

我有一个演示应用程序,我试图在其中模仿邮件应用程序的“新消息”交互式转换。这是邮件过渡的样子:

Mail app new message transition

请注意如何向下拖动以关闭,但是如果拖动的距离不够远,则会弹出视图并取消过渡。我可以在演示应用程序中复制过渡和交互性,但是我注意到取消取消过渡时,显示的视图控制器会重新设置动画,然后消失。看起来是这样的:

enter image description here

我最好的猜测是,由于某种原因,过渡上下文的容器视图已被删除,因为我向其中添加了呈现的视图控制器的视图。这是UIViewControllerAnimatedTransitioning对象内部的演示和关闭代码:

显示过渡

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    guard let fromVC = transitionContext.viewController(forKey: .from),
        let toVC = transitionContext.viewController(forKey: .to)
        else {
            return
    }

    let containerView = transitionContext.containerView
    let finalFrame = transitionContext.finalFrame(for: toVC)
    let duration = transitionDuration(using: transitionContext)
    let topSafeAreaSpace = fromVC.view.safeAreaInsets.top // using fromVC safe area since it's on screen and has correct insets
    let topGap: CGFloat = topSafeAreaSpace + 20

    containerView.addSubview(toVC.view)

    toVC.view.frame = CGRect(x: 0,
                             y: containerView.frame.height,
                             width: toVC.view.frame.width,
                             height: toVC.view.frame.height - 30)

    UIView.animate(withDuration: duration, animations: {
        toVC.view.frame = CGRect(x: finalFrame.minX,
                                 y: finalFrame.minY + topGap,
                                 width: finalFrame.width,
                                 height: finalFrame.height - topGap)

        let sideGap: CGFloat = 20
        fromVC.view.frame = CGRect(x: sideGap,
                                   y: topSafeAreaSpace,
                                   width: fromVC.view.frame.width - 2 * sideGap,
                                   height: fromVC.view.frame.height - 2 * topSafeAreaSpace)
        fromVC.view.layer.cornerRadius = 10
        fromVC.view.layoutIfNeeded()
    }) { _ in
        transitionContext.completeTransition(true)
    }
}

关闭转换

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    guard let fromVC = transitionContext.viewController(forKey: .from),
        let toVC = transitionContext.viewController(forKey: .to)
        else {
            return
    }

    let finalFrame = transitionContext.finalFrame(for: toVC)
    let duration = transitionDuration(using: transitionContext)

    UIView.animate(withDuration: duration, animations: {
        toVC.view.frame = finalFrame
        fromVC.view.frame = CGRect(x: fromVC.view.frame.minX,
                                   y: finalFrame.height,
                                   width: fromVC.view.frame.width,
                                   height: fromVC.view.frame.height)

        toVC.view.layer.cornerRadius = 0

        toVC.view.layoutIfNeeded()
    }) { _ in
        transitionContext.completeTransition(true)
    }
}

这是UIPercentDrivenInteractiveTransition对象中的代码:

func handleGesture(_ gesture: UIPanGestureRecognizer) {
    #warning("need to use superview?")
    let translation = gesture.translation(in: gesture.view)
    var progress = translation.y / 400
    progress = min(1, max(0, progress)) // constraining value between 1 and 0

    switch gesture.state {
    case .began:
        interactionInProgress = true
        viewController.dismiss(animated: true, completion: nil)
    case .changed:
        shouldCompleteTransition = progress > 0.5
        update(progress)
    case .cancelled:
        interactionInProgress = false
        cancel()
    case .ended:
        interactionInProgress = false
        if shouldCompleteTransition {
            finish()
        } else {
            cancel()
        }
    default:
        break
    }
}

任何帮助将不胜感激。值得注意的是,我将此Ray Wenderlich教程用作参考- https://www.raywenderlich.com/322-custom-uiviewcontroller-transitions-getting-started 但是,当他们使用图像快照为过渡设置动画时,我使用的是视图控制器的视图。

1 个答案:

答案 0 :(得分:0)

感谢@Dare的评论,我意识到所需要的只是对解雇动画完成块的一个小更新:

// before - broken

transitionContext.completeTransition(true)

// after - WORKING!

transitionContext.completeTransition(!transitionContext.transitionWasCancelled)