Skip to content

Commit

Permalink
- Adds support for presenting with any UIModalPresentationStyle (prev…
Browse files Browse the repository at this point in the history
…iously only fullscreen)
  • Loading branch information
alexhillc committed Jul 15, 2017
1 parent fd69d77 commit d432733
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
39 changes: 30 additions & 9 deletions Source/Classes/PhotosTransitionController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import FLAnimatedImage
fileprivate let FadeInOutTransitionRatio: Double = 1/3
fileprivate let TransitionAnimSpringDampening: CGFloat = 1

fileprivate static let supportedModalPresentationStyles: [UIModalPresentationStyle] = [.fullScreen,
.currentContext,
.custom,
.overFullScreen,
.overCurrentContext]

weak var delegate: PhotosTransitionControllerDelegate?
var mode: PhotosTransitionControllerMode = .presenting
var transitionInfo: TransitionInfo
Expand All @@ -40,7 +46,7 @@ import FLAnimatedImage
fileprivate var forceImmediateInteractiveDismissal = false
fileprivate var completeInteractiveDismissal = false

weak var dismissalTransitionContext: UIViewControllerContextTransitioning?
weak fileprivate var dismissalTransitionContext: UIViewControllerContextTransitioning?

weak fileprivate var imageView: UIImageView?
fileprivate var imageViewInitialCenter: CGPoint = .zero
Expand Down Expand Up @@ -70,6 +76,10 @@ import FLAnimatedImage
}
}

func supportsModalPresentationStyle(_ modalPresentationStyle: UIModalPresentationStyle) -> Bool {
return type(of: self).supportedModalPresentationStyles.contains(modalPresentationStyle)
}

init(photosViewController: PhotosViewController, transitionInfo: TransitionInfo) {
self.photosViewController = photosViewController
self.transitionInfo = transitionInfo
Expand Down Expand Up @@ -190,11 +200,17 @@ import FLAnimatedImage
return
}

if to.view.superview != transitionContext.containerView || from.view.superview != transitionContext.containerView {
if !transitionContext.isInteractive {
to.view.alpha = 0
}

let presentersViewRemoved = from.presentationController?.shouldRemovePresentersView ?? false
if to.view.superview != transitionContext.containerView && presentersViewRemoved {
to.view.frame = transitionContext.finalFrame(for: to)
transitionContext.containerView.addSubview(to.view)

}

if from.view.superview != transitionContext.containerView {
from.view.frame = transitionContext.finalFrame(for: from)
transitionContext.containerView.addSubview(from.view)
}
Expand Down Expand Up @@ -429,8 +445,12 @@ import FLAnimatedImage
}

to.view.alpha = 0
to.view.frame = transitionContext.finalFrame(for: to)
transitionContext.containerView.addSubview(to.view)

let presentersViewRemoved = from.presentationController?.shouldRemovePresentersView ?? false
if presentersViewRemoved {
to.view.frame = transitionContext.finalFrame(for: to)
transitionContext.containerView.addSubview(to.view)
}

from.view.frame = transitionContext.finalFrame(for: from)
transitionContext.containerView.addSubview(from.view)
Expand Down Expand Up @@ -467,15 +487,16 @@ import FLAnimatedImage

// MARK: - Interaction handling
@objc fileprivate func panAction(_ sender: UIPanGestureRecognizer) {
guard let photosViewController = self.photosViewController,
self.supportsModalPresentationStyle(photosViewController.modalPresentationStyle) else {
return
}

self.dismissalVelocityY = sender.velocity(in: sender.view).y
let translation = sender.translation(in: sender.view?.superview)

switch sender.state {
case .began:
guard let photosViewController = self.photosViewController else {
return
}

self.overlayView = nil
photosViewController.presentingViewController?.dismiss(animated: true, completion: {
sender.isEnabled = true
Expand Down
16 changes: 7 additions & 9 deletions Source/Classes/PhotosViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,9 @@ import MobileCoreServices
}

self.transitionInfo.resolveEndingViewClosure?(photo, self.currentPhotoIndex)
guard let transitionController = self.transitionController, transitionController.supportsContextualDismissal ||
transitionController.supportsInteractiveDismissal else {
guard let transitionController = self.transitionController, transitionController.supportsModalPresentationStyle(self.modalPresentationStyle) &&
(transitionController.supportsContextualDismissal ||
transitionController.supportsInteractiveDismissal) else {
return nil
}

Expand All @@ -259,7 +260,8 @@ import MobileCoreServices
}

public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
guard let transitionController = self.transitionController, transitionController.supportsContextualPresentation else {
guard let transitionController = self.transitionController, transitionController.supportsModalPresentationStyle(self.modalPresentationStyle) &&
transitionController.supportsContextualPresentation else {
return nil
}

Expand Down Expand Up @@ -388,12 +390,8 @@ import MobileCoreServices
}
}

if self.traitCollection.horizontalSizeClass == .compact {
self.present(activityViewController, animated: true)
} else {
activityViewController.popoverPresentationController?.barButtonItem = barButtonItem
self.present(activityViewController, animated: true)
}
activityViewController.popoverPresentationController?.barButtonItem = barButtonItem
self.present(activityViewController, animated: true)
}

@objc public func closeAction(_ sender: UIBarButtonItem) {
Expand Down

0 comments on commit d432733

Please sign in to comment.