2021-04-28 09:02:34 +02:00
|
|
|
//
|
|
|
|
// MediaHostToMediaPreviewViewControllerAnimatedTransitioning.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-4-28.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
2021-04-28 13:06:45 +02:00
|
|
|
import func AVFoundation.AVMakeRect
|
2021-04-28 09:02:34 +02:00
|
|
|
|
|
|
|
final class MediaHostToMediaPreviewViewControllerAnimatedTransitioning: ViewControllerAnimatedTransitioning {
|
|
|
|
|
|
|
|
let transitionItem: MediaPreviewTransitionItem
|
|
|
|
let panGestureRecognizer: UIPanGestureRecognizer
|
|
|
|
|
|
|
|
private var isTransitionContextFinish = false
|
|
|
|
|
|
|
|
private var popInteractiveTransitionAnimator = MediaHostToMediaPreviewViewControllerAnimatedTransitioning.animator(initialVelocity: .zero)
|
|
|
|
private var itemInteractiveTransitionAnimator = MediaHostToMediaPreviewViewControllerAnimatedTransitioning.animator(initialVelocity: .zero)
|
|
|
|
|
|
|
|
init(operation: UINavigationController.Operation, transitionItem: MediaPreviewTransitionItem, panGestureRecognizer: UIPanGestureRecognizer) {
|
|
|
|
self.transitionItem = transitionItem
|
|
|
|
self.panGestureRecognizer = panGestureRecognizer
|
|
|
|
super.init(operation: operation)
|
|
|
|
}
|
|
|
|
|
|
|
|
class func animator(initialVelocity: CGVector = .zero) -> UIViewPropertyAnimator {
|
|
|
|
let timingParameters = UISpringTimingParameters(mass: 4.0, stiffness: 1300, damping: 180, initialVelocity: initialVelocity)
|
|
|
|
return UIViewPropertyAnimator(duration: 0.5, timingParameters: timingParameters)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UIViewControllerAnimatedTransitioning
|
|
|
|
extension MediaHostToMediaPreviewViewControllerAnimatedTransitioning {
|
|
|
|
|
|
|
|
override func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
|
|
|
|
super.animateTransition(using: transitionContext)
|
|
|
|
|
|
|
|
switch operation {
|
|
|
|
case .push: pushTransition(using: transitionContext).startAnimation()
|
|
|
|
case .pop: popTransition(using: transitionContext).startAnimation()
|
|
|
|
default: return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func pushTransition(using transitionContext: UIViewControllerContextTransitioning, curve: UIView.AnimationCurve = .easeInOut) -> UIViewPropertyAnimator {
|
|
|
|
guard let toVC = transitionContext.viewController(forKey: .to) as? MediaPreviewViewController,
|
|
|
|
let toView = transitionContext.view(forKey: .to) else {
|
|
|
|
fatalError()
|
|
|
|
}
|
2021-04-28 13:06:45 +02:00
|
|
|
|
2021-04-28 09:02:34 +02:00
|
|
|
let toViewEndFrame = transitionContext.finalFrame(for: toVC)
|
|
|
|
toView.frame = toViewEndFrame
|
|
|
|
toView.alpha = 0
|
|
|
|
transitionContext.containerView.addSubview(toView)
|
2021-04-28 13:06:45 +02:00
|
|
|
// set to image hidden
|
2021-07-01 13:18:41 +02:00
|
|
|
toVC.pagingViewController.view.alpha = 0
|
2021-04-28 13:06:45 +02:00
|
|
|
// set from image hidden. update hidden when paging. seealso: `MediaPreviewViewController`
|
2021-04-28 14:10:17 +02:00
|
|
|
transitionItem.source.updateAppearance(position: .start, index: toVC.viewModel.currentPage.value)
|
2021-04-28 13:06:45 +02:00
|
|
|
|
|
|
|
// Set transition image view
|
|
|
|
assert(transitionItem.initialFrame != nil)
|
|
|
|
let initialFrame = transitionItem.initialFrame ?? toViewEndFrame
|
|
|
|
let transitionTargetFrame: CGRect = {
|
|
|
|
let aspectRatio = transitionItem.aspectRatio ?? CGSize(width: initialFrame.width, height: initialFrame.height)
|
|
|
|
return AVMakeRect(aspectRatio: aspectRatio, insideRect: toView.bounds)
|
|
|
|
}()
|
|
|
|
let transitionImageView: UIImageView = {
|
|
|
|
let imageView = UIImageView(frame: transitionContext.containerView.convert(initialFrame, from: nil))
|
|
|
|
imageView.clipsToBounds = true
|
|
|
|
imageView.contentMode = .scaleAspectFill
|
|
|
|
imageView.isUserInteractionEnabled = false
|
|
|
|
imageView.image = transitionItem.image
|
2021-05-10 10:06:00 +02:00
|
|
|
// accessibility
|
|
|
|
imageView.accessibilityIgnoresInvertColors = true
|
2021-04-28 13:06:45 +02:00
|
|
|
return imageView
|
|
|
|
}()
|
|
|
|
transitionItem.targetFrame = transitionTargetFrame
|
|
|
|
transitionItem.imageView = transitionImageView
|
|
|
|
transitionContext.containerView.addSubview(transitionImageView)
|
|
|
|
|
2021-05-11 10:11:22 +02:00
|
|
|
toVC.closeButtonBackground.alpha = 0
|
|
|
|
|
|
|
|
if UIAccessibility.isReduceTransparencyEnabled {
|
|
|
|
toVC.visualEffectView.alpha = 0
|
|
|
|
}
|
2021-04-28 09:02:34 +02:00
|
|
|
|
2021-05-11 10:11:22 +02:00
|
|
|
let animator = MediaHostToMediaPreviewViewControllerAnimatedTransitioning.animator(initialVelocity: .zero)
|
|
|
|
|
2021-04-28 09:02:34 +02:00
|
|
|
animator.addAnimations {
|
2021-04-28 13:06:45 +02:00
|
|
|
transitionImageView.frame = transitionTargetFrame
|
2021-04-28 09:02:34 +02:00
|
|
|
toView.alpha = 1
|
2021-05-11 10:11:22 +02:00
|
|
|
if UIAccessibility.isReduceTransparencyEnabled {
|
|
|
|
toVC.visualEffectView.alpha = 1
|
|
|
|
}
|
2021-04-28 09:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
animator.addCompletion { position in
|
2021-07-01 13:18:41 +02:00
|
|
|
toVC.pagingViewController.view.alpha = 1
|
2021-04-28 13:06:45 +02:00
|
|
|
transitionImageView.removeFromSuperview()
|
2021-05-11 10:11:22 +02:00
|
|
|
UIView.animate(withDuration: 0.33, delay: 0, options: [.curveEaseInOut]) {
|
|
|
|
toVC.closeButtonBackground.alpha = 1
|
|
|
|
}
|
2021-04-28 09:02:34 +02:00
|
|
|
transitionContext.completeTransition(position == .end)
|
|
|
|
}
|
|
|
|
|
|
|
|
return animator
|
|
|
|
}
|
|
|
|
|
|
|
|
private func popTransition(using transitionContext: UIViewControllerContextTransitioning, curve: UIView.AnimationCurve = .easeInOut) -> UIViewPropertyAnimator {
|
|
|
|
guard let fromVC = transitionContext.viewController(forKey: .from) as? MediaPreviewViewController,
|
2021-04-28 13:06:45 +02:00
|
|
|
let fromView = transitionContext.view(forKey: .from),
|
2021-07-01 13:18:41 +02:00
|
|
|
let mediaPreviewImageViewController = fromVC.pagingViewController.currentViewController as? MediaPreviewImageViewController,
|
|
|
|
let index = fromVC.pagingViewController.currentIndex else {
|
2021-04-28 09:02:34 +02:00
|
|
|
fatalError()
|
|
|
|
}
|
2021-04-28 13:06:45 +02:00
|
|
|
|
|
|
|
// assert view hierarchy not change
|
|
|
|
let toVC = transitionItem.previewableViewController
|
|
|
|
let targetFrame = toVC.sourceFrame(transitionItem: transitionItem, index: index)
|
|
|
|
|
|
|
|
let imageView = mediaPreviewImageViewController.previewImageView.imageView
|
|
|
|
let _snapshot: UIView? = {
|
|
|
|
transitionItem.snapshotRaw = imageView
|
|
|
|
let snapshot = imageView.snapshotView(afterScreenUpdates: false)
|
|
|
|
snapshot?.clipsToBounds = true
|
|
|
|
snapshot?.contentMode = .scaleAspectFill
|
|
|
|
return snapshot
|
|
|
|
}()
|
|
|
|
guard let snapshot = _snapshot else {
|
|
|
|
transitionContext.completeTransition(false)
|
|
|
|
fatalError()
|
|
|
|
}
|
2021-05-11 10:11:22 +02:00
|
|
|
|
|
|
|
let transitionMaskView = UIView(frame: transitionContext.containerView.bounds)
|
|
|
|
transitionMaskView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
|
|
transitionContext.containerView.addSubview(transitionMaskView)
|
|
|
|
|
|
|
|
let maskLayer = CAShapeLayer()
|
|
|
|
maskLayer.frame = transitionMaskView.bounds
|
|
|
|
let maskLayerFromPath = UIBezierPath(rect: maskLayer.bounds).cgPath
|
|
|
|
maskLayer.path = maskLayerFromPath
|
|
|
|
transitionMaskView.layer.mask = maskLayer
|
|
|
|
|
|
|
|
transitionMaskView.addSubview(snapshot)
|
|
|
|
snapshot.center = transitionMaskView.center
|
|
|
|
fromVC.view.bringSubviewToFront(fromVC.closeButtonBackground)
|
2021-04-28 13:06:45 +02:00
|
|
|
|
|
|
|
transitionItem.imageView = imageView
|
|
|
|
transitionItem.snapshotTransitioning = snapshot
|
|
|
|
transitionItem.initialFrame = snapshot.frame
|
|
|
|
transitionItem.targetFrame = targetFrame
|
2021-04-28 09:02:34 +02:00
|
|
|
|
2021-04-28 13:06:45 +02:00
|
|
|
// disable interaction
|
2021-07-01 13:18:41 +02:00
|
|
|
fromVC.pagingViewController.isUserInteractionEnabled = false
|
2021-04-28 13:06:45 +02:00
|
|
|
|
|
|
|
let animator = popInteractiveTransitionAnimator
|
2021-04-28 09:02:34 +02:00
|
|
|
|
2021-04-28 13:06:45 +02:00
|
|
|
self.transitionItem.snapshotRaw?.alpha = 0.0
|
2021-05-11 10:11:22 +02:00
|
|
|
|
|
|
|
var needsMaskWithAnimation = true
|
|
|
|
let maskLayerToRect: CGRect? = {
|
|
|
|
guard case .mosaic = transitionItem.source else { return nil }
|
2021-05-27 11:01:48 +02:00
|
|
|
guard let navigationBar = toVC.navigationController?.navigationBar, let navigationBarSuperView = navigationBar.superview else { return nil }
|
|
|
|
let navigationBarFrameInWindow = navigationBarSuperView.convert(navigationBar.frame, to: nil)
|
2021-05-11 10:11:22 +02:00
|
|
|
|
2021-05-27 11:01:48 +02:00
|
|
|
// crop rect top edge
|
|
|
|
var rect = transitionMaskView.frame
|
|
|
|
let _toViewFrameInWindow = toVC.view.superview.flatMap { $0.convert(toVC.view.frame, to: nil) }
|
|
|
|
if let toViewFrameInWindow = _toViewFrameInWindow, toViewFrameInWindow.minY > navigationBarFrameInWindow.maxY {
|
|
|
|
rect.origin.y = toViewFrameInWindow.minY
|
|
|
|
} else {
|
|
|
|
rect.origin.y = navigationBarFrameInWindow.maxY + UIView.separatorLineHeight(of: toVC.view) // extra hairline
|
|
|
|
}
|
|
|
|
|
2021-05-11 10:11:22 +02:00
|
|
|
if rect.minY < snapshot.frame.minY {
|
|
|
|
needsMaskWithAnimation = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return rect
|
|
|
|
}()
|
|
|
|
let maskLayerToPath = maskLayerToRect.flatMap { UIBezierPath(rect: $0) }?.cgPath
|
|
|
|
let maskLayerToFinalRect: CGRect? = {
|
|
|
|
guard case .mosaic = transitionItem.source else { return nil }
|
2021-05-27 11:01:48 +02:00
|
|
|
guard let tabBarController = toVC.tabBarController, let tabBarSuperView = tabBarController.tabBar.superview else { return nil }
|
|
|
|
let tabBarFrameInWindow = tabBarSuperView.convert(tabBarController.tabBar.frame, to: nil)
|
2021-05-11 10:11:22 +02:00
|
|
|
var rect = maskLayerToRect ?? transitionMaskView.frame
|
|
|
|
let offset = rect.maxY - tabBarFrameInWindow.minY
|
|
|
|
guard offset > 0 else { return rect }
|
|
|
|
rect.size.height -= offset
|
|
|
|
return rect
|
|
|
|
}()
|
|
|
|
let maskLayerToFinalPath = maskLayerToFinalRect.flatMap { UIBezierPath(rect: $0) }?.cgPath
|
|
|
|
|
|
|
|
if !needsMaskWithAnimation, let maskLayerToPath = maskLayerToPath {
|
|
|
|
maskLayer.path = maskLayerToPath
|
|
|
|
}
|
|
|
|
|
2021-04-28 09:02:34 +02:00
|
|
|
animator.addAnimations {
|
2021-04-28 13:06:45 +02:00
|
|
|
if let targetFrame = targetFrame {
|
|
|
|
self.transitionItem.snapshotTransitioning?.frame = targetFrame
|
|
|
|
} else {
|
|
|
|
fromView.alpha = 0
|
|
|
|
}
|
2021-04-28 14:10:17 +02:00
|
|
|
self.transitionItem.sourceImageViewCornerRadius.flatMap { self.transitionItem.snapshotTransitioning?.layer.cornerRadius = $0 }
|
2021-04-28 13:06:45 +02:00
|
|
|
fromVC.closeButtonBackground.alpha = 0
|
|
|
|
fromVC.visualEffectView.effect = nil
|
2021-05-11 10:11:22 +02:00
|
|
|
if let maskLayerToFinalPath = maskLayerToFinalPath {
|
|
|
|
maskLayer.path = maskLayerToFinalPath
|
|
|
|
}
|
|
|
|
if UIAccessibility.isReduceTransparencyEnabled {
|
|
|
|
fromVC.visualEffectView.alpha = 0
|
|
|
|
}
|
2021-04-28 09:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
animator.addCompletion { position in
|
2021-04-28 13:06:45 +02:00
|
|
|
self.transitionItem.snapshotTransitioning?.removeFromSuperview()
|
2021-04-28 14:10:17 +02:00
|
|
|
self.transitionItem.source.updateAppearance(position: position, index: nil)
|
2021-04-28 09:02:34 +02:00
|
|
|
transitionContext.completeTransition(position == .end)
|
|
|
|
}
|
|
|
|
|
|
|
|
return animator
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UIViewControllerInteractiveTransitioning
|
|
|
|
extension MediaHostToMediaPreviewViewControllerAnimatedTransitioning {
|
|
|
|
|
|
|
|
override func startInteractiveTransition(_ transitionContext: UIViewControllerContextTransitioning) {
|
|
|
|
super.startInteractiveTransition(transitionContext)
|
|
|
|
|
|
|
|
switch operation {
|
|
|
|
case .pop:
|
2021-04-28 13:06:45 +02:00
|
|
|
// Note: change item.imageView transform via pan gesture
|
2021-04-28 09:02:34 +02:00
|
|
|
panGestureRecognizer.addTarget(self, action: #selector(MediaHostToMediaPreviewViewControllerAnimatedTransitioning.updatePanGestureInteractive(_:)))
|
|
|
|
popInteractiveTransition(using: transitionContext)
|
|
|
|
default:
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func popInteractiveTransition(using transitionContext: UIViewControllerContextTransitioning) {
|
|
|
|
guard let fromVC = transitionContext.viewController(forKey: .from) as? MediaPreviewViewController,
|
2021-05-31 11:07:32 +02:00
|
|
|
let _ = transitionContext.view(forKey: .from),
|
2021-07-01 13:18:41 +02:00
|
|
|
let mediaPreviewImageViewController = fromVC.pagingViewController.currentViewController as? MediaPreviewImageViewController,
|
|
|
|
let index = fromVC.pagingViewController.currentIndex else {
|
2021-04-28 09:02:34 +02:00
|
|
|
fatalError()
|
|
|
|
}
|
2021-04-28 13:06:45 +02:00
|
|
|
|
|
|
|
// assert view hierarchy not change
|
|
|
|
let toVC = transitionItem.previewableViewController
|
|
|
|
let targetFrame = toVC.sourceFrame(transitionItem: transitionItem, index: index)
|
|
|
|
|
|
|
|
let imageView = mediaPreviewImageViewController.previewImageView.imageView
|
|
|
|
let _snapshot: UIView? = {
|
|
|
|
transitionItem.snapshotRaw = imageView
|
|
|
|
let snapshot = imageView.snapshotView(afterScreenUpdates: false)
|
|
|
|
snapshot?.clipsToBounds = true
|
|
|
|
snapshot?.contentMode = .scaleAspectFill
|
|
|
|
return snapshot
|
|
|
|
}()
|
|
|
|
guard let snapshot = _snapshot else {
|
|
|
|
transitionContext.completeTransition(false)
|
|
|
|
return
|
|
|
|
}
|
2021-05-11 10:11:22 +02:00
|
|
|
|
|
|
|
let transitionMaskView = UIView(frame: transitionContext.containerView.bounds)
|
|
|
|
transitionMaskView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
|
|
transitionContext.containerView.addSubview(transitionMaskView)
|
|
|
|
transitionItem.interactiveTransitionMaskView = transitionMaskView
|
|
|
|
|
|
|
|
let maskLayer = CAShapeLayer()
|
|
|
|
maskLayer.frame = transitionMaskView.bounds
|
|
|
|
maskLayer.path = UIBezierPath(rect: maskLayer.bounds).cgPath
|
|
|
|
transitionMaskView.layer.mask = maskLayer
|
|
|
|
transitionItem.interactiveTransitionMaskLayer = maskLayer
|
|
|
|
|
|
|
|
transitionMaskView.addSubview(snapshot)
|
|
|
|
snapshot.center = transitionMaskView.center
|
|
|
|
fromVC.view.bringSubviewToFront(fromVC.closeButtonBackground)
|
2021-04-28 09:02:34 +02:00
|
|
|
|
2021-04-28 13:06:45 +02:00
|
|
|
transitionItem.imageView = imageView
|
|
|
|
transitionItem.snapshotTransitioning = snapshot
|
|
|
|
transitionItem.initialFrame = snapshot.frame
|
2021-04-28 14:36:10 +02:00
|
|
|
transitionItem.targetFrame = targetFrame ?? snapshot.frame
|
2021-04-28 13:06:45 +02:00
|
|
|
|
|
|
|
// disable interaction
|
2021-07-01 13:18:41 +02:00
|
|
|
fromVC.pagingViewController.isUserInteractionEnabled = false
|
2021-04-28 13:06:45 +02:00
|
|
|
|
2021-04-28 09:02:34 +02:00
|
|
|
let animator = popInteractiveTransitionAnimator
|
|
|
|
|
|
|
|
let blurEffect = fromVC.visualEffectView.effect
|
|
|
|
self.transitionItem.snapshotRaw?.alpha = 0.0
|
2021-04-28 13:06:45 +02:00
|
|
|
|
2021-05-11 10:11:22 +02:00
|
|
|
UIView.animate(withDuration: 0.33, delay: 0, options: [.curveEaseInOut]) {
|
|
|
|
fromVC.closeButtonBackground.alpha = 0
|
|
|
|
}
|
|
|
|
|
2021-04-28 09:02:34 +02:00
|
|
|
animator.addAnimations {
|
2021-04-28 14:36:10 +02:00
|
|
|
switch self.transitionItem.source {
|
|
|
|
case .profileBanner:
|
|
|
|
self.transitionItem.snapshotTransitioning?.alpha = 0.4
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2021-04-28 09:02:34 +02:00
|
|
|
fromVC.visualEffectView.effect = nil
|
2021-04-28 14:10:17 +02:00
|
|
|
self.transitionItem.sourceImageViewCornerRadius.flatMap { self.transitionItem.snapshotTransitioning?.layer.cornerRadius = $0 }
|
2021-05-11 10:11:22 +02:00
|
|
|
if UIAccessibility.isReduceTransparencyEnabled {
|
|
|
|
fromVC.visualEffectView.alpha = 0
|
|
|
|
}
|
2021-04-28 09:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
animator.addCompletion { position in
|
2021-07-01 13:18:41 +02:00
|
|
|
fromVC.pagingViewController.isUserInteractionEnabled = true
|
2021-04-28 13:06:45 +02:00
|
|
|
fromVC.closeButtonBackground.alpha = position == .end ? 0 : 1
|
2021-04-28 09:02:34 +02:00
|
|
|
self.transitionItem.imageView?.isHidden = position == .end
|
|
|
|
self.transitionItem.snapshotRaw?.alpha = position == .start ? 1.0 : 0.0
|
|
|
|
self.transitionItem.snapshotTransitioning?.removeFromSuperview()
|
2021-04-28 13:06:45 +02:00
|
|
|
if position == .end {
|
2021-04-28 14:10:17 +02:00
|
|
|
// reset appearance
|
|
|
|
self.transitionItem.source.updateAppearance(position: position, index: nil)
|
2021-04-28 13:06:45 +02:00
|
|
|
}
|
2021-04-28 09:02:34 +02:00
|
|
|
fromVC.visualEffectView.effect = position == .end ? nil : blurEffect
|
2021-05-11 10:11:22 +02:00
|
|
|
transitionMaskView.removeFromSuperview()
|
|
|
|
UIView.animate(withDuration: 0.33, delay: 0, options: [.curveEaseInOut]) {
|
|
|
|
fromVC.closeButtonBackground.alpha = position == .end ? 0 : 1
|
|
|
|
}
|
|
|
|
if UIAccessibility.isReduceTransparencyEnabled {
|
|
|
|
fromVC.visualEffectView.alpha = position == .end ? 0 : 1
|
|
|
|
}
|
2021-04-28 09:02:34 +02:00
|
|
|
transitionContext.completeTransition(position == .end)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension MediaHostToMediaPreviewViewControllerAnimatedTransitioning {
|
|
|
|
|
|
|
|
@objc func updatePanGestureInteractive(_ sender: UIPanGestureRecognizer) {
|
|
|
|
guard !isTransitionContextFinish else { return } // do not accept transition abort
|
|
|
|
|
|
|
|
switch sender.state {
|
|
|
|
case .began, .changed:
|
|
|
|
let translation = sender.translation(in: transitionContext.containerView)
|
|
|
|
let percent = popInteractiveTransitionAnimator.fractionComplete + progressStep(for: translation)
|
|
|
|
popInteractiveTransitionAnimator.fractionComplete = percent
|
|
|
|
transitionContext.updateInteractiveTransition(percent)
|
|
|
|
updateTransitionItemPosition(of: translation)
|
|
|
|
|
|
|
|
// Reset translation to zero
|
|
|
|
sender.setTranslation(CGPoint.zero, in: transitionContext.containerView)
|
|
|
|
case .ended, .cancelled:
|
|
|
|
let targetPosition = completionPosition()
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: target position: %s", ((#file as NSString).lastPathComponent), #line, #function, targetPosition == .end ? "end" : "start")
|
|
|
|
isTransitionContextFinish = true
|
|
|
|
animate(targetPosition)
|
|
|
|
|
2021-04-28 13:06:45 +02:00
|
|
|
targetPosition == .end ? transitionContext.finishInteractiveTransition() : transitionContext.cancelInteractiveTransition()
|
2021-04-28 09:02:34 +02:00
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func convert(_ velocity: CGPoint, for item: MediaPreviewTransitionItem?) -> CGVector {
|
|
|
|
guard let currentFrame = item?.imageView?.frame, let targetFrame = item?.targetFrame else {
|
|
|
|
return CGVector.zero
|
|
|
|
}
|
|
|
|
|
|
|
|
let dx = abs(targetFrame.midX - currentFrame.midX)
|
|
|
|
let dy = abs(targetFrame.midY - currentFrame.midY)
|
|
|
|
|
|
|
|
guard dx > 0.0 && dy > 0.0 else {
|
|
|
|
return CGVector.zero
|
|
|
|
}
|
|
|
|
|
|
|
|
let range = CGFloat(35.0)
|
|
|
|
let clippedVx = clip(-range, range, velocity.x / dx)
|
|
|
|
let clippedVy = clip(-range, range, velocity.y / dy)
|
|
|
|
return CGVector(dx: clippedVx, dy: clippedVy)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func completionPosition() -> UIViewAnimatingPosition {
|
|
|
|
let completionThreshold: CGFloat = 0.33
|
|
|
|
let flickMagnitude: CGFloat = 1200 // pts/sec
|
|
|
|
let velocity = panGestureRecognizer.velocity(in: transitionContext.containerView).vector
|
|
|
|
let isFlick = (velocity.magnitude > flickMagnitude)
|
|
|
|
let isFlickDown = isFlick && (velocity.dy > 0.0)
|
|
|
|
let isFlickUp = isFlick && (velocity.dy < 0.0)
|
|
|
|
|
|
|
|
if (operation == .push && isFlickUp) || (operation == .pop && isFlickDown) {
|
|
|
|
return .end
|
|
|
|
} else if (operation == .push && isFlickDown) || (operation == .pop && isFlickUp) {
|
|
|
|
return .start
|
|
|
|
} else if popInteractiveTransitionAnimator.fractionComplete > completionThreshold {
|
|
|
|
return .end
|
|
|
|
} else {
|
|
|
|
return .start
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create item animator and start it
|
|
|
|
func animate(_ toPosition: UIViewAnimatingPosition) {
|
|
|
|
// Create a property animator to animate each image's frame change
|
|
|
|
let gestureVelocity = panGestureRecognizer.velocity(in: transitionContext.containerView)
|
|
|
|
let velocity = convert(gestureVelocity, for: transitionItem)
|
|
|
|
let itemAnimator = MediaHostToMediaPreviewViewControllerAnimatedTransitioning.animator(initialVelocity: velocity)
|
2021-05-11 10:11:22 +02:00
|
|
|
|
|
|
|
var maskLayerToFinalPath: CGPath?
|
|
|
|
if toPosition == .end,
|
|
|
|
let transitionMaskView = transitionItem.interactiveTransitionMaskView,
|
|
|
|
let snapshot = transitionItem.snapshotTransitioning {
|
|
|
|
let toVC = transitionItem.previewableViewController
|
|
|
|
|
|
|
|
var needsMaskWithAnimation = true
|
|
|
|
let maskLayerToRect: CGRect? = {
|
|
|
|
guard case .mosaic = transitionItem.source else { return nil }
|
2021-05-27 11:01:48 +02:00
|
|
|
guard let navigationBar = toVC.navigationController?.navigationBar, let navigationBarSuperView = navigationBar.superview else { return nil }
|
|
|
|
let navigationBarFrameInWindow = navigationBarSuperView.convert(navigationBar.frame, to: nil)
|
2021-05-11 10:11:22 +02:00
|
|
|
|
2021-05-27 11:01:48 +02:00
|
|
|
// crop rect top edge
|
|
|
|
var rect = transitionMaskView.frame
|
|
|
|
let _toViewFrameInWindow = toVC.view.superview.flatMap { $0.convert(toVC.view.frame, to: nil) }
|
|
|
|
if let toViewFrameInWindow = _toViewFrameInWindow, toViewFrameInWindow.minY > navigationBarFrameInWindow.maxY {
|
|
|
|
rect.origin.y = toViewFrameInWindow.minY
|
|
|
|
} else {
|
|
|
|
rect.origin.y = navigationBarFrameInWindow.maxY + UIView.separatorLineHeight(of: toVC.view) // extra hairline
|
|
|
|
}
|
|
|
|
|
2021-05-11 10:11:22 +02:00
|
|
|
if rect.minY < snapshot.frame.minY {
|
|
|
|
needsMaskWithAnimation = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return rect
|
|
|
|
}()
|
|
|
|
let maskLayerToPath = maskLayerToRect.flatMap { UIBezierPath(rect: $0) }?.cgPath
|
|
|
|
|
|
|
|
if let maskLayer = transitionItem.interactiveTransitionMaskLayer, !needsMaskWithAnimation {
|
|
|
|
maskLayer.path = maskLayerToPath
|
|
|
|
}
|
|
|
|
|
|
|
|
let maskLayerToFinalRect: CGRect? = {
|
|
|
|
guard case .mosaic = transitionItem.source else { return nil }
|
2021-05-27 11:01:48 +02:00
|
|
|
guard let tabBarController = toVC.tabBarController, let tabBarSuperView = tabBarController.tabBar.superview else { return nil }
|
|
|
|
let tabBarFrameInWindow = tabBarSuperView.convert(tabBarController.tabBar.frame, to: nil)
|
2021-05-11 10:11:22 +02:00
|
|
|
var rect = maskLayerToRect ?? transitionMaskView.frame
|
|
|
|
let offset = rect.maxY - tabBarFrameInWindow.minY
|
|
|
|
guard offset > 0 else { return rect }
|
|
|
|
rect.size.height -= offset
|
|
|
|
return rect
|
|
|
|
}()
|
|
|
|
maskLayerToFinalPath = maskLayerToFinalRect.flatMap { UIBezierPath(rect: $0) }?.cgPath
|
|
|
|
}
|
2021-04-28 09:02:34 +02:00
|
|
|
|
|
|
|
itemAnimator.addAnimations {
|
2021-05-11 10:11:22 +02:00
|
|
|
if let maskLayer = self.transitionItem.interactiveTransitionMaskLayer,
|
|
|
|
let maskLayerToFinalPath = maskLayerToFinalPath {
|
|
|
|
maskLayer.path = maskLayerToFinalPath
|
|
|
|
}
|
2021-04-28 09:02:34 +02:00
|
|
|
if toPosition == .end {
|
2021-04-28 14:36:10 +02:00
|
|
|
switch self.transitionItem.source {
|
|
|
|
case .profileBanner where toPosition == .end:
|
|
|
|
// fade transition for banner
|
2021-04-28 13:06:45 +02:00
|
|
|
self.transitionItem.snapshotTransitioning?.alpha = 0
|
2021-04-28 14:36:10 +02:00
|
|
|
default:
|
|
|
|
if let targetFrame = self.transitionItem.targetFrame {
|
|
|
|
self.transitionItem.snapshotTransitioning?.frame = targetFrame
|
|
|
|
} else {
|
|
|
|
self.transitionItem.snapshotTransitioning?.alpha = 0
|
|
|
|
}
|
2021-04-28 13:06:45 +02:00
|
|
|
}
|
2021-04-28 14:36:10 +02:00
|
|
|
|
2021-04-28 09:02:34 +02:00
|
|
|
} else {
|
2021-04-28 13:06:45 +02:00
|
|
|
if let initialFrame = self.transitionItem.initialFrame {
|
|
|
|
self.transitionItem.snapshotTransitioning?.frame = initialFrame
|
|
|
|
} else {
|
|
|
|
self.transitionItem.snapshotTransitioning?.alpha = 1
|
|
|
|
}
|
2021-04-28 09:02:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the property animator and keep track of it
|
|
|
|
self.itemInteractiveTransitionAnimator = itemAnimator
|
|
|
|
itemAnimator.startAnimation()
|
|
|
|
|
|
|
|
// Reverse the transition animator if we are returning to the start position
|
|
|
|
popInteractiveTransitionAnimator.isReversed = (toPosition == .start)
|
|
|
|
|
|
|
|
if popInteractiveTransitionAnimator.state == .inactive {
|
|
|
|
popInteractiveTransitionAnimator.startAnimation()
|
|
|
|
} else {
|
|
|
|
let durationFactor = CGFloat(itemAnimator.duration / popInteractiveTransitionAnimator.duration)
|
|
|
|
popInteractiveTransitionAnimator.continueAnimation(withTimingParameters: nil, durationFactor: durationFactor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func progressStep(for translation: CGPoint) -> CGFloat {
|
|
|
|
return (operation == .push ? -1.0 : 1.0) * translation.y / transitionContext.containerView.bounds.midY
|
|
|
|
}
|
|
|
|
|
|
|
|
private func updateTransitionItemPosition(of translation: CGPoint) {
|
|
|
|
let progress = progressStep(for: translation)
|
|
|
|
|
|
|
|
let initialSize = transitionItem.initialFrame!.size
|
2021-04-30 13:28:06 +02:00
|
|
|
guard initialSize != .zero else { return }
|
|
|
|
// assert(initialSize != .zero)
|
2021-04-28 09:02:34 +02:00
|
|
|
|
|
|
|
guard let snapshot = transitionItem.snapshotTransitioning,
|
|
|
|
let finalSize = transitionItem.targetFrame?.size else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if snapshot.frame.size == .zero {
|
|
|
|
snapshot.frame.size = initialSize
|
|
|
|
}
|
|
|
|
|
|
|
|
let currentSize = snapshot.frame.size
|
|
|
|
|
|
|
|
let itemPercentComplete = clip(-0.05, 1.05, (currentSize.width - initialSize.width) / (finalSize.width - initialSize.width) + progress)
|
|
|
|
let itemWidth = lerp(initialSize.width, finalSize.width, itemPercentComplete)
|
|
|
|
let itemHeight = lerp(initialSize.height, finalSize.height, itemPercentComplete)
|
|
|
|
assert(currentSize.width != 0.0)
|
|
|
|
assert(currentSize.height != 0.0)
|
|
|
|
let scaleTransform = CGAffineTransform(scaleX: (itemWidth / currentSize.width), y: (itemHeight / currentSize.height))
|
|
|
|
let scaledOffset = transitionItem.touchOffset.apply(transform: scaleTransform)
|
|
|
|
|
|
|
|
snapshot.center = (snapshot.center + (translation + (transitionItem.touchOffset - scaledOffset))).point
|
|
|
|
snapshot.bounds = CGRect(origin: CGPoint.zero, size: CGSize(width: itemWidth, height: itemHeight))
|
|
|
|
transitionItem.touchOffset = scaledOffset
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|