Scale evenly in both dimensions in gallery animations

This commit is contained in:
Shadowfacts 2024-03-29 18:58:31 -04:00
parent 4fcc32ca4b
commit c05107bccd
3 changed files with 6 additions and 3 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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([