From 3274b5f549b9e4cc8ab9f3e9734ee9fc6109e8e8 Mon Sep 17 00:00:00 2001 From: Michael Henry Pantaleon Date: Wed, 19 Oct 2022 02:53:26 +1100 Subject: [PATCH] fix: flaky transition, wait for the transition once the target image is ready. --- Sources/ImageViewer_swift/ImageLoader.swift | 2 -- .../ImageViewer_swift/ImageViewerController.swift | 1 - .../ImageViewerTransitionPresentationManager.swift | 14 ++++++++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Sources/ImageViewer_swift/ImageLoader.swift b/Sources/ImageViewer_swift/ImageLoader.swift index bef1855..d5b97db 100644 --- a/Sources/ImageViewer_swift/ImageLoader.swift +++ b/Sources/ImageViewer_swift/ImageLoader.swift @@ -22,10 +22,8 @@ public struct URLSessionImageLoader: ImageLoader { } DispatchQueue.main.async { - UIView.setAnimationsEnabled(false) imageView.image = image completion(image) - UIView.setAnimationsEnabled(true) } } } diff --git a/Sources/ImageViewer_swift/ImageViewerController.swift b/Sources/ImageViewer_swift/ImageViewerController.swift index 0221875..bad0b76 100644 --- a/Sources/ImageViewer_swift/ImageViewerController.swift +++ b/Sources/ImageViewer_swift/ImageViewerController.swift @@ -254,7 +254,6 @@ extension ImageViewerController { } func updateConstraintsForSize(_ size: CGSize) { - let yOffset = max(0, (size.height - imageView.frame.height) / 2) top.constant = yOffset bottom.constant = yOffset diff --git a/Sources/ImageViewer_swift/ImageViewerTransitionPresentationManager.swift b/Sources/ImageViewer_swift/ImageViewerTransitionPresentationManager.swift index bf0f70e..3802e66 100644 --- a/Sources/ImageViewer_swift/ImageViewerTransitionPresentationManager.swift +++ b/Sources/ImageViewer_swift/ImageViewerTransitionPresentationManager.swift @@ -21,6 +21,8 @@ final class ImageViewerTransitionPresentationAnimator:NSObject { let isPresenting: Bool let imageContentMode: UIView.ContentMode + + var observation: NSKeyValueObservation? init(isPresenting: Bool, imageContentMode: UIView.ContentMode) { self.isPresenting = isPresenting @@ -100,10 +102,14 @@ extension ImageViewerTransitionPresentationAnimator: UIViewControllerAnimatedTra UIView.animate(withDuration: duration, animations: { dummyImageView.frame = UIScreen.main.bounds controller.view.alpha = 1.0 - }) { finished in - transitionVC.targetView?.alpha = 1.0 - dummyImageView.removeFromSuperview() - completed(finished) + }) { [weak self] finished in + self?.observation = transitionVC.targetView?.observe(\.image, options: [.new, .initial]) { img, change in + if img.image != nil { + transitionVC.targetView?.alpha = 1.0 + dummyImageView.removeFromSuperview() + completed(finished) + } + } } }