From 2ab4433c65ec006d7c63a2d825d54aa3ffa45c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eddy=20Leli=C3=A8vre-Berna?= Date: Tue, 18 Jul 2023 22:42:58 +0200 Subject: [PATCH] Fixes #544 second suggestion: scrollView management modified so that pinching with two fingers triggers a return to the album view when the image is zoomed out --- piwigo/Album/ImageAnimatedTransitioning.swift | 2 +- piwigo/Image/ImagePreviewViewController.swift | 28 +++++++++++++++++-- piwigo/Image/ImageViewController.swift | 4 +-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/piwigo/Album/ImageAnimatedTransitioning.swift b/piwigo/Album/ImageAnimatedTransitioning.swift index 3790ee380..aa30c5f66 100644 --- a/piwigo/Album/ImageAnimatedTransitioning.swift +++ b/piwigo/Album/ImageAnimatedTransitioning.swift @@ -21,7 +21,7 @@ enum PresentationType { // See https://medium.com/@tungfam/custom-uiviewcontroller-transitions-in-swift-d1677e5aa0bf final class ImageAnimatedTransitioning: NSObject, UIViewControllerAnimatedTransitioning { - static let duration: TimeInterval = 0.5 + static let duration: TimeInterval = 0.2 private let type: PresentationType private let albumViewController: AlbumViewController diff --git a/piwigo/Image/ImagePreviewViewController.swift b/piwigo/Image/ImagePreviewViewController.swift index 412da4124..cfcefeaad 100644 --- a/piwigo/Image/ImagePreviewViewController.swift +++ b/piwigo/Image/ImagePreviewViewController.swift @@ -31,7 +31,7 @@ class ImagePreviewViewController: UIViewController private let placeHolder = UIImage(named: "placeholderImage")! private var userDidTapOnce: Bool = false // True if the user did tap the view private var userDidRotateDevice: Bool = false // True if the user did rotate the device - + private var startingZoomScale = CGFloat(1) // To remember the scale before starting zooming // MARK: - View Lifecycle override func viewDidLoad() { @@ -169,6 +169,10 @@ class ImagePreviewViewController: UIViewController scrollView.bounds = view.bounds scrollView.contentSize = image.size + // Prevents scrolling image at minimum scale + // Will be unlocked when starting zooming + scrollView.isScrollEnabled = false + // Don't adjust the insets when showing or hiding the navigation bar/toolbar scrollView.contentInset = .zero scrollView.contentInsetAdjustmentBehavior = .never @@ -340,14 +344,32 @@ extension ImagePreviewViewController: UIScrollViewDelegate func viewForZooming(in scrollView: UIScrollView) -> UIView? { return imageView } + + // Zooming of the content in the scroll view is about to commence + func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) { + // Allows scrolling zoomed image + scrollView.isScrollEnabled = true + // Store zoom scale value before starting zooming + startingZoomScale = scrollView.zoomScale + } + // The scroll view’s zoom factor changed func scrollViewDidZoom(_ scrollView: UIScrollView) { - centerImageView() + if scrollView.zoomScale < 0.9 * scrollView.minimumZoomScale, + startingZoomScale < 1.1 * scrollView.minimumZoomScale { + dismiss(animated: true) + } else { + centerImageView() + } } + // Zooming of the content in the scroll view completed func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) { // Limit the zoom scale - if scale < scrollView.minimumZoomScale { + if scrollView.zoomScale < 0.9 * scrollView.minimumZoomScale, + startingZoomScale < 1.1 * scrollView.minimumZoomScale { + dismiss(animated: true) + } else if scale <= scrollView.minimumZoomScale { scrollView.zoomScale = scrollView.minimumZoomScale centerImageView() } else if scale > scrollView.maximumZoomScale { diff --git a/piwigo/Image/ImageViewController.swift b/piwigo/Image/ImageViewController.swift index db44388a7..a41de82e2 100644 --- a/piwigo/Image/ImageViewController.swift +++ b/piwigo/Image/ImageViewController.swift @@ -105,7 +105,7 @@ class ImageViewController: UIViewController { tapOnce.require(toFail: tapTwice) // Down swipes return to album view - let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(swipeDown(_:))) + let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(didSwipeDown(_:))) swipeDown.numberOfTouchesRequired = 1 swipeDown.direction = .down tapOnce.require(toFail: swipeDown) @@ -578,7 +578,7 @@ class ImageViewController: UIViewController { } } - @objc func swipeDown(_ gestureRecognizer: UIGestureRecognizer) { + @objc func didSwipeDown(_ gestureRecognizer: UIGestureRecognizer) { // Return to the album view returnToAlbum() }