2021-04-28 09:02:34 +02:00
|
|
|
//
|
|
|
|
// MediaPreviewTransitionController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-4-28.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
final class MediaPreviewTransitionController: NSObject {
|
|
|
|
|
|
|
|
weak var mediaPreviewViewController: MediaPreviewViewController?
|
|
|
|
|
|
|
|
var wantsInteractiveStart = false
|
|
|
|
private var panGestureRecognizer: UIPanGestureRecognizer = {
|
|
|
|
let gestureRecognizer = UIPanGestureRecognizer()
|
|
|
|
gestureRecognizer.maximumNumberOfTouches = 1
|
|
|
|
return gestureRecognizer
|
|
|
|
}()
|
|
|
|
private var dismissInteractiveTransitioning: MediaHostToMediaPreviewViewControllerAnimatedTransitioning?
|
|
|
|
|
|
|
|
override init() {
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
panGestureRecognizer.delegate = self
|
|
|
|
panGestureRecognizer.addTarget(self, action: #selector(MediaPreviewTransitionController.panGestureRecognizerHandler(_:)))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension MediaPreviewTransitionController {
|
|
|
|
|
|
|
|
@objc private func panGestureRecognizerHandler(_ sender: UIPanGestureRecognizer) {
|
|
|
|
guard dismissInteractiveTransitioning == nil else { return }
|
|
|
|
|
|
|
|
guard let mediaPreviewViewController = self.mediaPreviewViewController else { return }
|
|
|
|
wantsInteractiveStart = true
|
|
|
|
mediaPreviewViewController.dismiss(animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UIGestureRecognizerDelegate
|
|
|
|
extension MediaPreviewTransitionController: UIGestureRecognizerDelegate {
|
|
|
|
|
|
|
|
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
|
2021-04-30 13:28:06 +02:00
|
|
|
if gestureRecognizer === panGestureRecognizer || otherGestureRecognizer === panGestureRecognizer {
|
2021-04-28 09:02:34 +02:00
|
|
|
// FIXME: should enable zoom up pan dismiss
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
|
|
|
|
if gestureRecognizer === panGestureRecognizer {
|
|
|
|
guard let mediaPreviewViewController = self.mediaPreviewViewController else { return false }
|
2021-07-01 13:18:41 +02:00
|
|
|
return mediaPreviewViewController.isInteractiveDismissible()
|
2021-04-28 09:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UIViewControllerTransitioningDelegate
|
|
|
|
extension MediaPreviewTransitionController: UIViewControllerTransitioningDelegate {
|
|
|
|
|
|
|
|
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
|
|
|
|
guard let mediaPreviewViewController = presented as? MediaPreviewViewController else {
|
|
|
|
assertionFailure()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
self.mediaPreviewViewController = mediaPreviewViewController
|
|
|
|
self.mediaPreviewViewController?.view.addGestureRecognizer(panGestureRecognizer)
|
|
|
|
|
|
|
|
return MediaHostToMediaPreviewViewControllerAnimatedTransitioning(
|
|
|
|
operation: .push,
|
2022-01-27 14:23:39 +01:00
|
|
|
transitionItem: mediaPreviewViewController.viewModel.transitionItem,
|
2021-04-28 09:02:34 +02:00
|
|
|
panGestureRecognizer: panGestureRecognizer
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func interactionControllerForPresentation(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
|
|
|
|
// not support interactive present
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
|
|
|
|
guard let mediaPreviewViewController = dismissed as? MediaPreviewViewController else {
|
|
|
|
assertionFailure()
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-28 13:06:45 +02:00
|
|
|
|
2021-04-28 09:02:34 +02:00
|
|
|
return MediaHostToMediaPreviewViewControllerAnimatedTransitioning(
|
|
|
|
operation: .pop,
|
2022-01-27 14:23:39 +01:00
|
|
|
transitionItem: mediaPreviewViewController.viewModel.transitionItem,
|
2021-04-28 09:02:34 +02:00
|
|
|
panGestureRecognizer: panGestureRecognizer
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
|
|
|
|
guard let transitioning = animator as? MediaHostToMediaPreviewViewControllerAnimatedTransitioning,
|
|
|
|
transitioning.operation == .pop, wantsInteractiveStart else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dismissInteractiveTransitioning = transitioning
|
|
|
|
transitioning.delegate = self
|
|
|
|
return transitioning
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - ViewControllerAnimatedTransitioningDelegate
|
|
|
|
extension MediaPreviewTransitionController: ViewControllerAnimatedTransitioningDelegate {
|
|
|
|
|
|
|
|
func animationEnded(_ transitionCompleted: Bool) {
|
|
|
|
dismissInteractiveTransitioning = nil
|
|
|
|
wantsInteractiveStart = false
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|