From fa828a5eaef2e49dcbde9be0bdbba0593291f444 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 25 Nov 2024 18:38:07 -0500 Subject: [PATCH] Gallery dismiss interaction fixes --- .../GalleryVC/GalleryDismissInteraction.swift | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/Packages/GalleryVC/Sources/GalleryVC/GalleryDismissInteraction.swift b/Packages/GalleryVC/Sources/GalleryVC/GalleryDismissInteraction.swift index 3bff7aef..d7153b30 100644 --- a/Packages/GalleryVC/Sources/GalleryVC/GalleryDismissInteraction.swift +++ b/Packages/GalleryVC/Sources/GalleryVC/GalleryDismissInteraction.swift @@ -20,6 +20,8 @@ class GalleryDismissInteraction: NSObject { private(set) var dismissVelocity: CGPoint? private(set) var dismissTranslation: CGPoint? + private var cancelAnimator: UIViewPropertyAnimator? + init(viewController: GalleryViewController) { self.viewController = viewController super.init() @@ -38,6 +40,8 @@ class GalleryDismissInteraction: NSObject { content = viewController.currentItemViewController.takeContent() content!.view.translatesAutoresizingMaskIntoConstraints = true content!.view.frame = origContentFrameInGallery! + // Make sure the context remains behind the controls + content!.view.layer.zPosition = -1000 viewController.view.addSubview(content!.view) origControlsVisible = viewController.currentItemViewController.controlsVisible @@ -59,20 +63,27 @@ class GalleryDismissInteraction: NSObject { if translationMagnitude < 150 && velocityMagnitude < 500 { isActive = false + cancelAnimator?.stopAnimation(true) + let spring = UISpringTimingParameters(mass: 1, stiffness: 439, damping: 42, initialVelocity: .zero) - let animator = UIViewPropertyAnimator(duration: 0.2, timingParameters: spring) - animator.addAnimations { + cancelAnimator = UIViewPropertyAnimator(duration: 0.2, timingParameters: spring) + cancelAnimator!.addAnimations { self.content!.view.frame = self.origContentFrameInGallery! self.viewController.currentItemViewController.setControlsVisible(self.origControlsVisible!, animated: false, dueToUserInteraction: false) } - animator.addCompletion { _ in + cancelAnimator!.addCompletion { _ in + guard !self.isActive else { + // bail in case the animation finishing raced with the user's interaction + return + } + self.content!.view.layer.zPosition = 0 self.content!.view.removeFromSuperview() self.viewController.currentItemViewController.addContent() self.content = nil self.origContentFrameInGallery = nil self.origControlsVisible = nil } - animator.startAnimation() + cancelAnimator!.startAnimation() } else { dismissVelocity = velocity