33import UIKit
44
55public protocol DrawerConfiguration : class {
6- func topPositionY( in parentFrame : CGRect ) -> CGFloat
7- var bottomPositionHeight : CGFloat { get }
6+ func topPositionY( for parentHeight : CGFloat ) -> CGFloat
7+ func bottomPositionY ( for parentHeight : CGFloat ) -> CGFloat
88 /**
99 drawerDismissClosure is injected by this lib.
1010 You should not change it, and you should call it when user presses dismiss button in your content view controller.
@@ -84,7 +84,7 @@ extension UIViewController {
8484 return { [ weak self, weak containerView, weak initialDisplayAnimator] in
8585 guard let strongSelf = self , let strongContainerView = containerView else { return }
8686 let oldTopPositionY = panGestureTarget. topPositionY!
87- let oldBasePositionY = panGestureTarget. basePositionY
87+ let oldBasePositionY = panGestureTarget. basePositionY!
8888 panGestureTarget. refreshDrawerPositions ( )
8989 let newContainerHeight = strongSelf. view. bounds. height - panGestureTarget. topPositionY + bottomOverpullPaddingHeight
9090 if strongContainerView. frame. height != newContainerHeight {
@@ -97,15 +97,18 @@ extension UIViewController {
9797 let newTopPositionIsDifferentThanOld = Int ( panGestureTarget. topPositionY) != Int ( oldTopPositionY)
9898 let isOnOldBottomPosition = Int ( strongContainerView. frame. minY) == Int ( oldBasePositionY)
9999 let newBottomPositionIsDifferentThanOld = Int ( panGestureTarget. basePositionY) != Int ( oldBasePositionY)
100- if isOnOldTopPosition && newTopPositionIsDifferentThanOld {
101- self ? . updateWithNewYPosition ( panGestureTarget. topPositionY,
102- containerView: strongContainerView,
103- inFlightAnimator: initialDisplayAnimator
100+ let isInitialDisplayAnimation = initialDisplayAnimator != nil
101+ if !isInitialDisplayAnimation && isOnOldTopPosition && newTopPositionIsDifferentThanOld {
102+ self ? . updateDrawerPosition (
103+ inFlightAnimator: initialDisplayAnimator,
104+ newTargetY: panGestureTarget. topPositionY,
105+ containerView: strongContainerView
104106 )
105107 } else if isOnOldBottomPosition && newBottomPositionIsDifferentThanOld {
106- self ? . updateWithNewYPosition ( panGestureTarget. basePositionY,
107- containerView: strongContainerView,
108- inFlightAnimator: initialDisplayAnimator
108+ self ? . updateDrawerPosition (
109+ inFlightAnimator: initialDisplayAnimator,
110+ newTargetY: panGestureTarget. basePositionY,
111+ containerView: strongContainerView
109112 )
110113 }
111114 }
@@ -122,10 +125,10 @@ extension UIViewController {
122125 controller. didMove ( toParentViewController: self )
123126 }
124127
125- private func updateWithNewYPosition ( _ newY : CGFloat , containerView : UIView , inFlightAnimator : UIViewPropertyAnimator ? ) {
128+ private func updateDrawerPosition ( inFlightAnimator : UIViewPropertyAnimator ? , newTargetY : CGFloat , containerView : UIView ) {
126129 dLog ( " inFlightAnimator: \( String ( describing: inFlightAnimator) ) " )
127130 var newFrame = containerView. frame
128- newFrame. origin. y = newY
131+ newFrame. origin. y = newTargetY
129132 if let animator = inFlightAnimator {
130133 animator. addAnimations { containerView. frame = newFrame }
131134 } else {
@@ -137,7 +140,7 @@ extension UIViewController {
137140 containerView: UIView ,
138141 positionDelegate: DrawerPositionDelegate ? ) -> UIViewPropertyAnimator {
139142 var endFrame = containerView. frame
140- endFrame. origin. y = view. bounds. height - drawerConfiguration . bottomPositionHeight
143+ endFrame. origin. y = drawerConfiguration . bottomPositionY ( for : view. bounds. height)
141144 let animator = UIViewPropertyAnimator ( duration: 0.25 , curve: . easeInOut, animations: {
142145 containerView. frame = endFrame
143146 } )
@@ -184,7 +187,7 @@ extension UIViewController {
184187private extension UIView {
185188 func addContainerView( for drawerConfiguration: DrawerConfiguration , cornerRadius: CGFloat , bottomPaddingHeight: CGFloat ) -> UIView {
186189 var startFrame = bounds
187- startFrame. size. height = bounds. height - drawerConfiguration. topPositionY ( in : bounds) + bottomPaddingHeight
190+ startFrame. size. height = bounds. height - drawerConfiguration. topPositionY ( for : bounds. height ) + bottomPaddingHeight
188191 startFrame. origin. y += bounds. height
189192 let containerView = UIView ( frame: startFrame)
190193 containerView. preservesSuperviewLayoutMargins = true
0 commit comments