From 5567bdd44721a81253c987a9285bfcdfa41597d4 Mon Sep 17 00:00:00 2001 From: CMK Date: Thu, 1 Jul 2021 19:18:41 +0800 Subject: [PATCH] fix: add tolerance for media preview dismiss gesture trigger --- .../xcschemes/xcschememanagement.plist | 2 +- .../MediaPreviewViewController.swift | 40 +++++++++---------- ...wViewControllerAnimatedTransitioning.swift | 18 ++++----- .../MediaPreviewTransitionController.swift | 2 +- .../MediaPreviewingViewController.swift | 2 +- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist b/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist index fdbacc128..f1135b12e 100644 --- a/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist @@ -12,7 +12,7 @@ CoreDataStack.xcscheme_^#shared#^_ orderHint - 27 + 20 Mastodon - ASDK.xcscheme_^#shared#^_ diff --git a/Mastodon/Scene/MediaPreview/MediaPreviewViewController.swift b/Mastodon/Scene/MediaPreview/MediaPreviewViewController.swift index c9cbf744a..a0afe1ccd 100644 --- a/Mastodon/Scene/MediaPreview/MediaPreviewViewController.swift +++ b/Mastodon/Scene/MediaPreview/MediaPreviewViewController.swift @@ -21,7 +21,7 @@ final class MediaPreviewViewController: UIViewController, NeedsDependency { var viewModel: MediaPreviewViewModel! let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .systemMaterial)) - let pagingViewConttroller = MediaPreviewPagingViewController() + let pagingViewController = MediaPreviewPagingViewController() let closeButtonBackground: UIVisualEffectView = { let backgroundView = UIVisualEffectView(effect: UIBlurEffect(style: .systemUltraThinMaterial)) @@ -57,16 +57,16 @@ extension MediaPreviewViewController { visualEffectView.frame = view.bounds view.addSubview(visualEffectView) - pagingViewConttroller.view.translatesAutoresizingMaskIntoConstraints = false - addChild(pagingViewConttroller) - visualEffectView.contentView.addSubview(pagingViewConttroller.view) + pagingViewController.view.translatesAutoresizingMaskIntoConstraints = false + addChild(pagingViewController) + visualEffectView.contentView.addSubview(pagingViewController.view) NSLayoutConstraint.activate([ - visualEffectView.topAnchor.constraint(equalTo: pagingViewConttroller.view.topAnchor), - visualEffectView.bottomAnchor.constraint(equalTo: pagingViewConttroller.view.bottomAnchor), - visualEffectView.leadingAnchor.constraint(equalTo: pagingViewConttroller.view.leadingAnchor), - visualEffectView.trailingAnchor.constraint(equalTo: pagingViewConttroller.view.trailingAnchor), + visualEffectView.topAnchor.constraint(equalTo: pagingViewController.view.topAnchor), + visualEffectView.bottomAnchor.constraint(equalTo: pagingViewController.view.bottomAnchor), + visualEffectView.leadingAnchor.constraint(equalTo: pagingViewController.view.leadingAnchor), + visualEffectView.trailingAnchor.constraint(equalTo: pagingViewController.view.trailingAnchor), ]) - pagingViewConttroller.didMove(toParent: self) + pagingViewController.didMove(toParent: self) closeButtonBackground.translatesAutoresizingMaskIntoConstraints = false view.addSubview(closeButtonBackground) @@ -90,9 +90,9 @@ extension MediaPreviewViewController { viewModel.mediaPreviewImageViewControllerDelegate = self - pagingViewConttroller.interPageSpacing = 10 - pagingViewConttroller.delegate = self - pagingViewConttroller.dataSource = viewModel + pagingViewController.interPageSpacing = 10 + pagingViewController.delegate = self + pagingViewController.dataSource = viewModel closeButton.addTarget(self, action: #selector(MediaPreviewViewController.closeButtonPressed(_:)), for: .touchUpInside) @@ -128,8 +128,8 @@ extension MediaPreviewViewController { // MARK: - MediaPreviewingViewController extension MediaPreviewViewController: MediaPreviewingViewController { - func isInteractiveDismissable() -> Bool { - if let mediaPreviewImageViewController = pagingViewConttroller.currentViewController as? MediaPreviewImageViewController { + func isInteractiveDismissible() -> Bool { + if let mediaPreviewImageViewController = pagingViewController.currentViewController as? MediaPreviewImageViewController { let previewImageView = mediaPreviewImageViewController.previewImageView // TODO: allow zooming pan dismiss guard previewImageView.zoomScale == previewImageView.minimumZoomScale else { @@ -138,12 +138,12 @@ extension MediaPreviewViewController: MediaPreviewingViewController { let safeAreaInsets = previewImageView.safeAreaInsets let statusBarFrameHeight = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0 - let dismissable = previewImageView.contentOffset.y <= -(safeAreaInsets.top - statusBarFrameHeight) - os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: dismissable %s", ((#file as NSString).lastPathComponent), #line, #function, dismissable ? "true" : "false") - return dismissable + let dismissible = previewImageView.contentOffset.y <= -(safeAreaInsets.top - statusBarFrameHeight) + 3 // add 3pt tolerance + os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: dismissible %s", ((#file as NSString).lastPathComponent), #line, #function, dismissible ? "true" : "false") + return dismissible } - os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: dismissable false", ((#file as NSString).lastPathComponent), #line, #function) + os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: dismissible false", ((#file as NSString).lastPathComponent), #line, #function) return false } @@ -303,11 +303,11 @@ extension MediaPreviewViewController { } @objc private func showNextKeyCommandHandler(_ sender: UIKeyCommand) { - pagingViewConttroller.scrollToPage(.next, animated: true, completion: nil) + pagingViewController.scrollToPage(.next, animated: true, completion: nil) } @objc private func showPreviousKeyCommandHandler(_ sender: UIKeyCommand) { - pagingViewConttroller.scrollToPage(.previous, animated: true, completion: nil) + pagingViewController.scrollToPage(.previous, animated: true, completion: nil) } } diff --git a/Mastodon/Scene/Transition/MediaPreview/MediaHostToMediaPreviewViewControllerAnimatedTransitioning.swift b/Mastodon/Scene/Transition/MediaPreview/MediaHostToMediaPreviewViewControllerAnimatedTransitioning.swift index 50a518dc7..2daeba041 100644 --- a/Mastodon/Scene/Transition/MediaPreview/MediaHostToMediaPreviewViewControllerAnimatedTransitioning.swift +++ b/Mastodon/Scene/Transition/MediaPreview/MediaHostToMediaPreviewViewControllerAnimatedTransitioning.swift @@ -56,7 +56,7 @@ extension MediaHostToMediaPreviewViewControllerAnimatedTransitioning { toView.alpha = 0 transitionContext.containerView.addSubview(toView) // set to image hidden - toVC.pagingViewConttroller.view.alpha = 0 + toVC.pagingViewController.view.alpha = 0 // set from image hidden. update hidden when paging. seealso: `MediaPreviewViewController` transitionItem.source.updateAppearance(position: .start, index: toVC.viewModel.currentPage.value) @@ -98,7 +98,7 @@ extension MediaHostToMediaPreviewViewControllerAnimatedTransitioning { } animator.addCompletion { position in - toVC.pagingViewConttroller.view.alpha = 1 + toVC.pagingViewController.view.alpha = 1 transitionImageView.removeFromSuperview() UIView.animate(withDuration: 0.33, delay: 0, options: [.curveEaseInOut]) { toVC.closeButtonBackground.alpha = 1 @@ -112,8 +112,8 @@ extension MediaHostToMediaPreviewViewControllerAnimatedTransitioning { private func popTransition(using transitionContext: UIViewControllerContextTransitioning, curve: UIView.AnimationCurve = .easeInOut) -> UIViewPropertyAnimator { guard let fromVC = transitionContext.viewController(forKey: .from) as? MediaPreviewViewController, let fromView = transitionContext.view(forKey: .from), - let mediaPreviewImageViewController = fromVC.pagingViewConttroller.currentViewController as? MediaPreviewImageViewController, - let index = fromVC.pagingViewConttroller.currentIndex else { + let mediaPreviewImageViewController = fromVC.pagingViewController.currentViewController as? MediaPreviewImageViewController, + let index = fromVC.pagingViewController.currentIndex else { fatalError() } @@ -154,7 +154,7 @@ extension MediaHostToMediaPreviewViewControllerAnimatedTransitioning { transitionItem.targetFrame = targetFrame // disable interaction - fromVC.pagingViewConttroller.isUserInteractionEnabled = false + fromVC.pagingViewController.isUserInteractionEnabled = false let animator = popInteractiveTransitionAnimator @@ -246,8 +246,8 @@ extension MediaHostToMediaPreviewViewControllerAnimatedTransitioning { private func popInteractiveTransition(using transitionContext: UIViewControllerContextTransitioning) { guard let fromVC = transitionContext.viewController(forKey: .from) as? MediaPreviewViewController, let _ = transitionContext.view(forKey: .from), - let mediaPreviewImageViewController = fromVC.pagingViewConttroller.currentViewController as? MediaPreviewImageViewController, - let index = fromVC.pagingViewConttroller.currentIndex else { + let mediaPreviewImageViewController = fromVC.pagingViewController.currentViewController as? MediaPreviewImageViewController, + let index = fromVC.pagingViewController.currentIndex else { fatalError() } @@ -289,7 +289,7 @@ extension MediaHostToMediaPreviewViewControllerAnimatedTransitioning { transitionItem.targetFrame = targetFrame ?? snapshot.frame // disable interaction - fromVC.pagingViewConttroller.isUserInteractionEnabled = false + fromVC.pagingViewController.isUserInteractionEnabled = false let animator = popInteractiveTransitionAnimator @@ -315,7 +315,7 @@ extension MediaHostToMediaPreviewViewControllerAnimatedTransitioning { } animator.addCompletion { position in - fromVC.pagingViewConttroller.isUserInteractionEnabled = true + fromVC.pagingViewController.isUserInteractionEnabled = true fromVC.closeButtonBackground.alpha = position == .end ? 0 : 1 self.transitionItem.imageView?.isHidden = position == .end self.transitionItem.snapshotRaw?.alpha = position == .start ? 1.0 : 0.0 diff --git a/Mastodon/Scene/Transition/MediaPreview/MediaPreviewTransitionController.swift b/Mastodon/Scene/Transition/MediaPreview/MediaPreviewTransitionController.swift index 225a83209..bd5781b0b 100644 --- a/Mastodon/Scene/Transition/MediaPreview/MediaPreviewTransitionController.swift +++ b/Mastodon/Scene/Transition/MediaPreview/MediaPreviewTransitionController.swift @@ -56,7 +56,7 @@ extension MediaPreviewTransitionController: UIGestureRecognizerDelegate { func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { if gestureRecognizer === panGestureRecognizer { guard let mediaPreviewViewController = self.mediaPreviewViewController else { return false } - return mediaPreviewViewController.isInteractiveDismissable() + return mediaPreviewViewController.isInteractiveDismissible() } return false diff --git a/Mastodon/Scene/Transition/MediaPreview/MediaPreviewingViewController.swift b/Mastodon/Scene/Transition/MediaPreview/MediaPreviewingViewController.swift index 9c6e56d05..d74c99798 100644 --- a/Mastodon/Scene/Transition/MediaPreview/MediaPreviewingViewController.swift +++ b/Mastodon/Scene/Transition/MediaPreview/MediaPreviewingViewController.swift @@ -8,5 +8,5 @@ import Foundation protocol MediaPreviewingViewController: AnyObject { - func isInteractiveDismissable() -> Bool + func isInteractiveDismissible() -> Bool }