Skip to content

Commit

Permalink
Fixes #544 second suggestion: scrollView management modified so that …
Browse files Browse the repository at this point in the history
…pinching with two fingers triggers a return to the album view when the image is zoomed out
  • Loading branch information
EddyLB committed Jul 18, 2023
1 parent c011956 commit 2ab4433
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion piwigo/Album/ImageAnimatedTransitioning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 25 additions & 3 deletions piwigo/Image/ImagePreviewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions piwigo/Image/ImageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -578,7 +578,7 @@ class ImageViewController: UIViewController {
}
}

@objc func swipeDown(_ gestureRecognizer: UIGestureRecognizer) {
@objc func didSwipeDown(_ gestureRecognizer: UIGestureRecognizer) {
// Return to the album view
returnToAlbum()
}
Expand Down

0 comments on commit 2ab4433

Please sign in to comment.