From c05107bccdcf73536dbba22dc7907fb918f14bc4 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 29 Mar 2024 18:58:31 -0400 Subject: [PATCH] Scale evenly in both dimensions in gallery animations --- .../Sources/GalleryVC/GalleryDismissAnimationController.swift | 3 ++- .../GalleryVC/GalleryPresentationAnimationController.swift | 4 +++- .../Screens/Gallery/ImageGalleryContentViewController.swift | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Packages/GalleryVC/Sources/GalleryVC/GalleryDismissAnimationController.swift b/Packages/GalleryVC/Sources/GalleryVC/GalleryDismissAnimationController.swift index 7cb40574..c15fc4ea 100644 --- a/Packages/GalleryVC/Sources/GalleryVC/GalleryDismissAnimationController.swift +++ b/Packages/GalleryVC/Sources/GalleryVC/GalleryDismissAnimationController.swift @@ -43,9 +43,10 @@ class GalleryDismissAnimationController: NSObject, UIViewControllerAnimatedTrans let appliedSourceToDestTransform: Bool if destFrameInContainer.width > 0 && destFrameInContainer.height > 0 { appliedSourceToDestTransform = true + let scale = min(destFrameInContainer.width / sourceFrameInContainer.width, destFrameInContainer.height / sourceFrameInContainer.height) let sourceToDestTransform = origSourceTransform .translatedBy(x: destFrameInContainer.midX - sourceFrameInContainer.midX, y: destFrameInContainer.midY - sourceFrameInContainer.midY) - .scaledBy(x: destFrameInContainer.width / sourceFrameInContainer.width, y: destFrameInContainer.height / sourceFrameInContainer.height) + .scaledBy(x: scale, y: scale) sourceView.transform = sourceToDestTransform } else { appliedSourceToDestTransform = false diff --git a/Packages/GalleryVC/Sources/GalleryVC/GalleryPresentationAnimationController.swift b/Packages/GalleryVC/Sources/GalleryVC/GalleryPresentationAnimationController.swift index 29dd5143..f844a33a 100644 --- a/Packages/GalleryVC/Sources/GalleryVC/GalleryPresentationAnimationController.swift +++ b/Packages/GalleryVC/Sources/GalleryVC/GalleryPresentationAnimationController.swift @@ -49,9 +49,11 @@ class GalleryPresentationAnimationController: NSObject, UIViewControllerAnimated let origSourceTransform = sourceView.transform let sourceToDestTransform: CGAffineTransform? if destFrameInContainer.width > 0 && destFrameInContainer.height > 0 { + // Scale evenly in both dimensions, to prevent the source view appearing to stretch/distort during the animation. + let scale = min(destFrameInContainer.width / sourceFrameInContainer.width, destFrameInContainer.height / sourceFrameInContainer.height) sourceToDestTransform = origSourceTransform .translatedBy(x: destFrameInContainer.midX - sourceFrameInContainer.midX, y: destFrameInContainer.midY - sourceFrameInContainer.midY) - .scaledBy(x: destFrameInContainer.width / sourceFrameInContainer.width, y: destFrameInContainer.height / sourceFrameInContainer.height) + .scaledBy(x: scale, y: scale) } else { sourceToDestTransform = nil } diff --git a/Tusker/Screens/Gallery/ImageGalleryContentViewController.swift b/Tusker/Screens/Gallery/ImageGalleryContentViewController.swift index a8721153..e723d102 100644 --- a/Tusker/Screens/Gallery/ImageGalleryContentViewController.swift +++ b/Tusker/Screens/Gallery/ImageGalleryContentViewController.swift @@ -57,7 +57,7 @@ class ImageGalleryContentViewController: UIViewController, GalleryContentViewCon imageView = GIFImageView(image: maybeGrayscaleImage) imageView.translatesAutoresizingMaskIntoConstraints = false - imageView.contentMode = .scaleAspectFit + imageView.contentMode = .scaleAspectFill imageView.isUserInteractionEnabled = true view.addSubview(imageView) NSLayoutConstraint.activate([