Skip to content

Commit

Permalink
Fix animated transition issues when presenter is formSheet (zvonicek#378
Browse files Browse the repository at this point in the history
)
  • Loading branch information
zvonicek committed Jun 21, 2020
1 parent d5db87e commit db30f4c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions Example/ImageSlideshow/TableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class TableViewController: UITableViewController {

if let cell = tableView.cellForRow(at: indexPath), let imageView = cell.imageView {
slideshowTransitioningDelegate = ZoomAnimatedTransitioningDelegate(imageView: imageView, slideshowController: fullScreenController)
fullScreenController.modalPresentationStyle = .custom
fullScreenController.transitioningDelegate = slideshowTransitioningDelegate
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ open class FullScreenSlideshowViewController: UIViewController {
convenience init() {
self.init(nibName:nil, bundle:nil)

self.modalPresentationStyle = .custom
if #available(iOS 13.0, *) {
self.modalPresentationStyle = .fullScreen
// Use KVC to set the value to preserve backwards compatiblity with Xcode < 11
self.setValue(true, forKey: "modalInPresentation")
}
Expand Down Expand Up @@ -94,6 +94,9 @@ open class FullScreenSlideshowViewController: UIViewController {
super.viewWillDisappear(animated)

slideshow.slideshowItems.forEach { $0.cancelPendingLoad() }

// Prevents broken dismiss transition when image is zoomed in
slideshow.currentSlideshowItem?.zoomOut()
}

open override func viewDidLayoutSubviews() {
Expand Down
6 changes: 1 addition & 5 deletions ImageSlideshow/Classes/Core/ImageSlideshow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,7 @@ open class ImageSlideshow: UIView {
fullscreen.inputs = images
slideshowTransitioningDelegate = ZoomAnimatedTransitioningDelegate(slideshowView: self, slideshowController: fullscreen)
fullscreen.transitioningDelegate = slideshowTransitioningDelegate
if #available(iOS 13.0, *) {
// The modalPresentationStyle is already set in FullScreenSlideshowViewController, but multiple users
// reported that they need the `modalPresentationStyle` explicitly specified here.
fullscreen.modalPresentationStyle = .fullScreen
}
fullscreen.modalPresentationStyle = .custom
controller.present(fullscreen, animated: true, completion: completion)

return fullscreen
Expand Down
26 changes: 14 additions & 12 deletions ImageSlideshow/Classes/Core/ZoomAnimatedTransitioning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ open class ZoomAnimatedTransitioningDelegate: NSObject, UIViewControllerTransiti
open func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return interactionController
}

public func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return PresentationController(presentedViewController: presented, presenting: presenting)
}
}

private class PresentationController: UIPresentationController {
// Needed for interactive dismiss to keep the presenter View Controller visible
override var shouldRemovePresentersView: Bool {
return false
}
}

extension ZoomAnimatedTransitioningDelegate: UIGestureRecognizerDelegate {
Expand Down Expand Up @@ -198,7 +209,6 @@ class ZoomInAnimator: ZoomAnimator, UIViewControllerAnimatedTransitioning {
#else
containerView.sendSubview(toBack: transitionBackgroundView)
#endif


let finalFrame = toViewController.view.frame

Expand Down Expand Up @@ -273,14 +283,6 @@ class ZoomOutAnimator: ZoomAnimator, UIViewControllerAnimatedTransitioning {

let containerView = transitionContext.containerView

toViewController.view.frame = transitionContext.finalFrame(for: toViewController)
toViewController.view.alpha = 0
containerView.addSubview(toViewController.view)
#if swift(>=4.2)
containerView.sendSubviewToBack(toViewController.view)
#else
containerView.sendSubview(toBack: toViewController.view)
#endif
var transitionViewInitialFrame: CGRect
if let currentSlideshowItem = fromViewController.slideshow.currentSlideshowItem {
if let image = currentSlideshowItem.imageView.image {
Expand Down Expand Up @@ -322,16 +324,16 @@ class ZoomOutAnimator: ZoomAnimator, UIViewControllerAnimatedTransitioning {
containerView.sendSubview(toBack: transitionBackgroundView)
#endif

let transitionView: UIImageView = UIImageView(image: fromViewController.slideshow.currentSlideshowItem?.imageView.image)
let transitionView = UIImageView(image: fromViewController.slideshow.currentSlideshowItem?.imageView.image)
transitionView.contentMode = UIViewContentMode.scaleAspectFill
transitionView.clipsToBounds = true
transitionView.frame = transitionViewInitialFrame
containerView.addSubview(transitionView)
fromViewController.view.isHidden = true

let duration: TimeInterval = transitionDuration(using: transitionContext)
let duration = transitionDuration(using: transitionContext)
let animations = {
toViewController.view.alpha = 1
transitionBackgroundView.alpha = 0
transitionView.frame = transitionViewFinalFrame
}
let completion = { (_: Any) in
Expand Down

0 comments on commit db30f4c

Please sign in to comment.