Skip to content

Commit

Permalink
Allow opting out of appearance transition callbacks on presenting vie…
Browse files Browse the repository at this point in the history
…w controller (#47)
  • Loading branch information
nolanw committed Apr 14, 2021
1 parent b2f5bd7 commit 9274d55
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
29 changes: 20 additions & 9 deletions PanModal/Animator/PanModalPresentationAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ public class PanModalPresentationAnimator: NSObject {

let presentable = panModalLayoutType(from: transitionContext)

// Calls viewWillAppear and viewWillDisappear
fromVC.beginAppearanceTransition(false, animated: true)
let isAppearanceTransition = presentable?.isAppearanceTransition ?? true
if isAppearanceTransition {
// Calls viewWillAppear and viewWillDisappear
fromVC.beginAppearanceTransition(false, animated: true)
}

// Presents the view in shortForm position, initially
let yPos: CGFloat = presentable?.shortFormYPos ?? 0.0
Expand All @@ -92,8 +95,10 @@ public class PanModalPresentationAnimator: NSObject {
PanModalAnimator.animate({
panView.frame.origin.y = yPos
}, config: presentable) { [weak self] didComplete in
// Calls viewDidAppear and viewDidDisappear
fromVC.endAppearanceTransition()
if isAppearanceTransition {
// Calls viewDidAppear and viewDidDisappear
fromVC.endAppearanceTransition()
}
transitionContext.completeTransition(didComplete)
self?.feedbackGenerator = nil
}
Expand All @@ -109,18 +114,24 @@ public class PanModalPresentationAnimator: NSObject {
let fromVC = transitionContext.viewController(forKey: .from)
else { return }

// Calls viewWillAppear and viewWillDisappear
toVC.beginAppearanceTransition(true, animated: true)

let presentable = panModalLayoutType(from: transitionContext)

let isAppearanceTransition = presentable?.isAppearanceTransition ?? true
if isAppearanceTransition {
// Calls viewWillAppear and viewWillDisappear
toVC.beginAppearanceTransition(true, animated: true)
}

let panView: UIView = transitionContext.containerView.panContainerView ?? fromVC.view

PanModalAnimator.animate({
panView.frame.origin.y = transitionContext.containerView.frame.height
}, config: presentable) { didComplete in
fromVC.view.removeFromSuperview()
// Calls viewDidAppear and viewDidDisappear
toVC.endAppearanceTransition()
if isAppearanceTransition {
// Calls viewDidAppear and viewDidDisappear
toVC.endAppearanceTransition()
}
transitionContext.completeTransition(didComplete)
}
}
Expand Down
4 changes: 4 additions & 0 deletions PanModal/Presentable/PanModalPresentable+Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ public extension PanModalPresentable where Self: UIViewController {
return shouldRoundTopCorners
}

var isAppearanceTransition: Bool {
return true
}

func shouldRespond(to panModalGestureRecognizer: UIPanGestureRecognizer) -> Bool {
return true
}
Expand Down
9 changes: 9 additions & 0 deletions PanModal/Presentable/PanModalPresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ public protocol PanModalPresentable: AnyObject {
*/
var showDragIndicator: Bool { get }

/**
A flag to determine whether view lifecycle methods `view(Will|Did)(A|Disa)ppear` are called on the presenting view controller.

Since pan modal presentation does not remove the presenting view from the view hierarchy, it may not be desirable to call the relevant methods that are usually associated with removing the presenting view from the view hierarchy.

Default value is true.
*/
var isAppearanceTransition: Bool { get }

/**
Asks the delegate if the pan modal should respond to the pan modal gesture recognizer.

Expand Down
1 change: 1 addition & 0 deletions Tests/PanModalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class PanModalTests: XCTestCase {
XCTAssertEqual(vc.shouldRoundTopCorners, false)
XCTAssertEqual(vc.showDragIndicator, false)
XCTAssertEqual(vc.shouldRoundTopCorners, false)
XCTAssertEqual(vc.isAppearanceTransition, true)
XCTAssertEqual(vc.cornerRadius, 8.0)
XCTAssertEqual(vc.transitionDuration, PanModalAnimator.Constants.defaultTransitionDuration)
XCTAssertEqual(vc.transitionAnimationOptions, [.curveEaseInOut, .allowUserInteraction, .beginFromCurrentState])
Expand Down

0 comments on commit 9274d55

Please sign in to comment.