From 0c9f6e02bd6f4ee6b1c809d732159edc77849526 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 13 Dec 2022 14:14:13 -0500 Subject: [PATCH] Fix controls reappearing when swiping between pages in gallery --- .../LargeImageViewController.swift | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Tusker/Screens/Large Image/LargeImageViewController.swift b/Tusker/Screens/Large Image/LargeImageViewController.swift index 428b7442..fd22ca0f 100644 --- a/Tusker/Screens/Large Image/LargeImageViewController.swift +++ b/Tusker/Screens/Large Image/LargeImageViewController.swift @@ -47,6 +47,7 @@ class LargeImageViewController: UIViewController, UIScrollViewDelegate, LargeIma var shrinkGestureEnabled = true private var isInitialAppearance = true + private var skipUpdatingControlsWhileZooming = false private var prevZoomScale: CGFloat? private var isGrayscale = false private var contentViewSizeObservation: NSKeyValueObservation? @@ -216,9 +217,11 @@ class LargeImageViewController: UIViewController, UIScrollViewDelegate, LargeIma let heightScale = maxHeight / contentView.intrinsicContentSize.height let widthScale = view.bounds.width / contentView.intrinsicContentSize.width let minScale = min(widthScale, heightScale) + skipUpdatingControlsWhileZooming = true scrollView.minimumZoomScale = minScale scrollView.zoomScale = minScale scrollView.maximumZoomScale = minScale >= 1 ? minScale + 2 : 2 + skipUpdatingControlsWhileZooming = false centerImage() @@ -248,6 +251,12 @@ class LargeImageViewController: UIViewController, UIScrollViewDelegate, LargeIma } } + override func viewSafeAreaInsetsDidChange() { + super.viewSafeAreaInsetsDidChange() + // the controls view transforms take the safe area insets into account, so they need to be updated + updateControlsView() + } + override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) @@ -285,7 +294,7 @@ class LargeImageViewController: UIViewController, UIScrollViewDelegate, LargeIma } func updateControlsView() { - let topOffset = self.controlsVisible ? 0 : -self.topControlsView.bounds.height + let topOffset = self.controlsVisible ? 0 : -(self.topControlsView.bounds.height + self.view.safeAreaInsets.top) self.topControlsView.transform = CGAffineTransform(translationX: 0, y: topOffset) if self.imageDescription != nil { let bottomOffset = self.controlsVisible ? 0 : self.descriptionTextView.bounds.height + self.view.safeAreaInsets.bottom @@ -299,10 +308,12 @@ class LargeImageViewController: UIViewController, UIScrollViewDelegate, LargeIma func scrollViewDidZoom(_ scrollView: UIScrollView) { let prevZoomScale = self.prevZoomScale ?? scrollView.minimumZoomScale - if scrollView.zoomScale <= scrollView.minimumZoomScale { - setControlsVisible(true, animated: true) - } else if scrollView.zoomScale > prevZoomScale { - setControlsVisible(false, animated: true) + if !skipUpdatingControlsWhileZooming { + if scrollView.zoomScale <= scrollView.minimumZoomScale { + setControlsVisible(true, animated: true) + } else if scrollView.zoomScale > prevZoomScale { + setControlsVisible(false, animated: true) + } } self.prevZoomScale = scrollView.zoomScale }