From 56d12295bab1de34b70ad83d04cd2d615432e3b6 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 24 Nov 2024 23:58:39 -0500 Subject: [PATCH] Don't dismiss gallery if pan distance/velocity is low See #520 --- .../GalleryVC/GalleryDismissInteraction.swift | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/Packages/GalleryVC/Sources/GalleryVC/GalleryDismissInteraction.swift b/Packages/GalleryVC/Sources/GalleryVC/GalleryDismissInteraction.swift index e7d1ee364..3bff7aef2 100644 --- a/Packages/GalleryVC/Sources/GalleryVC/GalleryDismissInteraction.swift +++ b/Packages/GalleryVC/Sources/GalleryVC/GalleryDismissInteraction.swift @@ -53,12 +53,35 @@ class GalleryDismissInteraction: NSObject { let translation = recognizer.translation(in: viewController.view) let velocity = recognizer.velocity(in: viewController.view) - dismissVelocity = velocity - dismissTranslation = translation - viewController.dismiss(animated: true) + let translationMagnitude = sqrt(translation.x.magnitudeSquared + translation.y.magnitudeSquared) + let velocityMagnitude = sqrt(velocity.x.magnitudeSquared + velocity.y.magnitudeSquared) + + if translationMagnitude < 150 && velocityMagnitude < 500 { + isActive = false + + let spring = UISpringTimingParameters(mass: 1, stiffness: 439, damping: 42, initialVelocity: .zero) + let animator = UIViewPropertyAnimator(duration: 0.2, timingParameters: spring) + animator.addAnimations { + self.content!.view.frame = self.origContentFrameInGallery! + self.viewController.currentItemViewController.setControlsVisible(self.origControlsVisible!, animated: false, dueToUserInteraction: false) + } + animator.addCompletion { _ in + self.content!.view.removeFromSuperview() + self.viewController.currentItemViewController.addContent() + self.content = nil + self.origContentFrameInGallery = nil + self.origControlsVisible = nil + } + animator.startAnimation() - // don't unset this until after dismiss is called, so that the dismiss animation controller can read it - isActive = false + } else { + dismissVelocity = velocity + dismissTranslation = translation + viewController.dismiss(animated: true) + + // don't unset this until after dismiss is called, so that the dismiss animation controller can read it + isActive = false + } default: break