@@ -59,16 +59,7 @@ final class DashboardViewController: UIViewController {
5959 /// When we hide the header, we disable this constraint so the content view can grow to fill the screen
6060 private var contentTopToHeaderConstraint : NSLayoutConstraint ?
6161
62- private lazy var hideHeaderAnimator = {
63- let animator = UIViewPropertyAnimator ( duration: Constants . animationDurationSeconds, curve: . easeOut)
64- animator. pausesOnCompletion = true
65- animator. addAnimations { [ weak self] in
66- self ? . contentTopToHeaderConstraint? . isActive = false
67- self ? . headerStackView. alpha = 0
68- self ? . view. layoutIfNeeded ( )
69- }
70- return animator
71- } ( )
62+ private var headerAnimator : UIViewPropertyAnimator ?
7263
7364 // Used to trick the navigation bar for large title (ref: issue 3 in p91TBi-45c-p2).
7465 private let hiddenScrollView = UIScrollView ( )
@@ -164,22 +155,42 @@ final class DashboardViewController: UIViewController {
164155 return true
165156 }
166157
167- func startHeaderAnimation( ) {
168- hideHeaderAnimator. startAnimation ( )
158+ func showHeader( ) {
159+ contentTopToHeaderConstraint? . isActive = true
160+ headerStackView. alpha = 1
161+ view. layoutIfNeeded ( )
169162 }
170163
171- func hideHeaderWithAnimation( ) {
172- hideHeaderAnimator. isReversed = false
173- hideHeaderAnimator. startAnimation ( )
164+ func hideHeader( ) {
165+ contentTopToHeaderConstraint? . isActive = false
166+ headerStackView. alpha = 0
167+ view. layoutIfNeeded ( )
174168 }
175169
176170 func showHeaderWithAnimation( ) {
177- // If the view hasn't been hidden yet, don't bother trying to animate showing it
178- guard hideHeaderAnimator. state == . active else {
179- return
180- }
181- hideHeaderAnimator. isReversed = true
182- hideHeaderAnimator. startAnimation ( )
171+ headerAnimator? . stopAnimation ( true )
172+ headerAnimator = UIViewPropertyAnimator . runningPropertyAnimator (
173+ withDuration: Constants . animationDurationSeconds,
174+ delay: 0 ,
175+ animations: { [ weak self] in
176+ self ? . showHeader ( )
177+ } ,
178+ completion: { [ weak self] position in
179+ self ? . headerAnimator = nil
180+ } )
181+ }
182+
183+ func hideHeaderWithAnimation( ) {
184+ headerAnimator? . stopAnimation ( true )
185+ headerAnimator = UIViewPropertyAnimator . runningPropertyAnimator (
186+ withDuration: Constants . animationDurationSeconds,
187+ delay: 0 ,
188+ animations: { [ weak self] in
189+ self ? . hideHeader ( )
190+ } ,
191+ completion: { [ weak self] position in
192+ self ? . headerAnimator = nil
193+ } )
183194 }
184195}
185196
0 commit comments