UIViewController 交互式过渡 - 取消交互式关闭时呈现的视图消失

     2023-03-10     121

关键词:

【中文标题】UIViewController 交互式过渡 - 取消交互式关闭时呈现的视图消失【英文标题】:UIViewController interactive transition - Presented view disappears when interactive dismiss is canceled 【发布时间】:2019-01-09 18:58:03 【问题描述】:

我有一个演示应用程序,我试图在其中模仿邮件应用程序“新消息”交互式转换 - 您可以向下拖动以关闭,但如果您没有拖得足够远,视图会弹回并且转换是取消。我能够在我的演示应用程序中复制过渡和交互性,但是我注意到当取消过渡被取消时,呈现的视图控制器被动画回原位,然后消失。这是它的样子:

我最好的猜测是转换上下文的容器视图由于某种原因被删除了,因为我向其中添加了呈现的视图控制器的视图。这是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 然而,在他们使用图像快照为过渡设置动画的地方,我使用了视图控制器的视图。

【问题讨论】:

您是否尝试过将 false 传递给 transitionContext.completeTransition(_:)?根据文档,“如果到呈现的视图控制器的转换成功完成,则为 true,如果仍在显示原始视图控制器,则为 false。” developer.apple.com/documentation/uikit/… 哇,我的疏忽了。谢谢@Dare! 【参考方案1】:

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

// before - broken

transitionContext.completeTransition(true)

// after - WORKING!

transitionContext.completeTransition(!transitionContext.transitionWasCancelled)

【讨论】:

同时 UIViewController 过渡动画

】同时UIViewController过渡动画【英文标题】:SimultaneousUIViewControllerTransitionAnimations【发布时间】:2018-11-0600:15:56【问题描述】:我正在尝试淡入背景渐变图像并将uiview卡片从底部(屏幕外)滑到uiviewcontroller的中心-当uiviewcontroller... 查看详情

UIViewController – 自定义关闭过渡的问题

】UIViewController–自定义关闭过渡的问题【英文标题】:UIViewController–issuewithcustomdismisstransition【发布时间】:2014-06-0615:46:22【问题描述】:总结我有一个内容UIViewController,它使用自定义转换呈现设置UIViewController。演示文稿是pre... 查看详情

UIPageViewController 从子 UIViewController 上的控制过渡

】UIPageViewController从子UIViewController上的控制过渡【英文标题】:UIPageViewControllertransitionfromcontrolonchildUIViewController【发布时间】:2015-08-2417:36:45【问题描述】:我有一个PageViewController,它有两个UIViewController,它们的设置如下:View... 查看详情

过渡到已经包含的 UIViewController

】过渡到已经包含的UIViewController【英文标题】:TransitioningtoanalreadycontainedUIViewController【发布时间】:2015-09-2820:35:41【问题描述】:我有一个带有多个“卡片”的垂直UIScrollView,嵌套的UIViewControllers(通过容器视图),每个都有... 查看详情

从 UIViewController 过渡到 TabBarViewController

】从UIViewController过渡到TabBarViewController【英文标题】:TransitionfromUIViewControllertoTabBarViewController【发布时间】:2016-04-1011:05:18【问题描述】:我已经搜索了6个小时的解决方案,但没有找到适合我情况的解决方案。我的故事板遵循... 查看详情

带有旋转的自定义 UIViewController 过渡

】带有旋转的自定义UIViewController过渡【英文标题】:CustomUIViewControllertransitionwithrotation【发布时间】:2011-05-0719:17:29【问题描述】:我有几个UIViewController子类。一个显示网格,另一个本质上是网格上图块的“详细视图”。用户... 查看详情

UIViewController 默认过渡动画不起作用

】UIViewController默认过渡动画不起作用【英文标题】:UIViewControllerdefaulttransitionanimationnotworking【发布时间】:2017-03-2322:14:42【问题描述】:我试图在navigationController中以模态方式呈现viewController,然后在x-seconds之后推送另一个视图... 查看详情

UIViewController 过渡在下拉手势关闭时闪烁

】UIViewController过渡在下拉手势关闭时闪烁【英文标题】:UIViewControllertransitionflickersondismissalwithpull-downgesture【发布时间】:2017-10-2713:02:50【问题描述】:当用户使用自定义转换单击MKMapView中的注释时,我正在呈现一个视图控制器... 查看详情

从 Tableview 自定义单元格翻转过渡到 UIViewController

】从Tableview自定义单元格翻转过渡到UIViewController【英文标题】:FlipTransitionfromaTableviewCustomCelltoaUIViewController【发布时间】:2013-12-0504:44:40【问题描述】:我创建了一个自定义的tableview单元格,它显示在我的tableview中。在用户点... 查看详情

自定义 UIViewController 过渡动画适用于演示,但不适用于解雇

】自定义UIViewController过渡动画适用于演示,但不适用于解雇【英文标题】:CustomUIViewControllertransitionanimationworksonpresentationbutnotondismissal【发布时间】:2018-04-1820:34:19【问题描述】:我有UINavigationControllerA,它是UISplitViewController的... 查看详情

UIViewController 动画过渡在 iOS8 中失败

】UIViewController动画过渡在iOS8中失败【英文标题】:UIViewControllerAnimatedTransitioningfailsiniOS8【发布时间】:2014-09-1623:19:33【问题描述】:我有一个自定义UIViewController转换-在iOS7中,它工作得非常好。但是,在iOS8上运行时,我遇到了... 查看详情

为啥 [UIViewController presentModalViewController:animated:] 从左侧过渡模态视图?

】为啥[UIViewControllerpresentModalViewController:animated:]从左侧过渡模态视图?【英文标题】:Whyis[UIViewControllerpresentModalViewController:animated:]transitioningthemodalviewinfromtheleft?为什么[UIViewControllerpresentModalViewController:a 查看详情

在 2 个 UIViewController 的过渡之间为 UIView 元素设置动画

】在2个UIViewController的过渡之间为UIView元素设置动画【英文标题】:AnimateUIViewelementsbetweentransitionof2UIViewControllers【发布时间】:2013-09-0819:07:35【问题描述】:我有2个UIViewControllers,其中包含类似的UIView组件。例如,第一个UIViewCon... 查看详情

UIViewController“后退”过渡期间,UINaviagtionBar 子视图不会淡出

】UIViewController“后退”过渡期间,UINaviagtionBar子视图不会淡出【英文标题】:UINaviagtionBarsubviewdoesn\'tfadeoutduringUIViewController"Back"transition【发布时间】:2014-03-1018:15:04【问题描述】:我有一个基于UINavigationBar的应用程序。... 查看详情

UIViewController 自定义过渡风格(由内而外呈现)

】UIViewController自定义过渡风格(由内而外呈现)【英文标题】:UIViewControllercustomtransitionstyle(insideoutpresentation)【发布时间】:2017-02-1620:15:22【问题描述】:我想制作一个自定义演示文稿(推送或模态)以在我搜索的“由内而外... 查看详情

非全屏和全屏 UIViewController 之间的漂亮幻灯片过渡

】非全屏和全屏UIViewController之间的漂亮幻灯片过渡【英文标题】:Niceslidetransitionbetweennon-fullscreenandfullscreenUIViewController【发布时间】:2011-12-1112:45:37【问题描述】:我有一个不是全屏的视图控制器(有一个状态栏),并且想要... 查看详情

iOS7,有没有办法在 UINavigationViewcontroller 中禁用 UIViewController 的过渡?

】iOS7,有没有办法在UINavigationViewcontroller中禁用UIViewController的过渡?【英文标题】:iOS7,IsthereawaytodisablethetransitionofUIViewControllerinUINavigationViewcontroller?【发布时间】:2013-11-2005:12:14【问题描述】:我在导航控制器中有一个视图控... 查看详情

如何使 UIPageViewController 过渡行为类似于 IOS7 中的交互式弹出过渡样式

】如何使UIPageViewController过渡行为类似于IOS7中的交互式弹出过渡样式【英文标题】:HowtomakeUIPageViewControllertransitionbehaviourliketheinteractivepoptransitionstyleinIOS7【发布时间】:2014-08-2815:27:32【问题描述】:在IOS7中,当我们在屏幕边缘... 查看详情