gpt4 book ai didi

ios - UIViewController 交互式转换 - 当交互式关闭被取消时,呈现的 View 消失

转载 作者:搜寻专家 更新时间:2023-11-01 06:26:17 24 4
gpt4 key购买 nike

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

enter image description here

我最好的猜测是过渡上下文的容器 View 由于某种原因被删除了,因为我向它添加了呈现的 View Controller 的 View 。以下是 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但是,在他们使用图像快照为过渡设置动画的地方,我使用的是 View Controller 的 View 。

最佳答案

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

// before - broken

transitionContext.completeTransition(true)

// after - WORKING!

transitionContext.completeTransition(!transitionContext.transitionWasCancelled)

关于ios - UIViewController 交互式转换 - 当交互式关闭被取消时,呈现的 View 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54116615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com