From ed6740c158d1fb57acd2e6a32993d18d6cbf2dd3 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Wed, 16 Feb 2022 08:32:04 -0800 Subject: [PATCH] tune the scrolling behavior of nav bar --- .../Sources/Views/Article/WebAppViewCoordinator.swift | 11 ++++++++--- .../Views/LinkedItemDetail/LinkItemDetailView.swift | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Views/Article/WebAppViewCoordinator.swift b/apple/OmnivoreKit/Sources/Views/Article/WebAppViewCoordinator.swift index c8f9837bd..79bd6c40b 100644 --- a/apple/OmnivoreKit/Sources/Views/Article/WebAppViewCoordinator.swift +++ b/apple/OmnivoreKit/Sources/Views/Article/WebAppViewCoordinator.swift @@ -10,6 +10,7 @@ final class WebAppViewCoordinator: NSObject { private var yOffsetAtStartOfDrag: Double? private var lastYOffset: Double = 0 private var hasDragged = false + private var isNavBarHidden = false override init() { super.init() @@ -48,20 +49,23 @@ extension WebAppViewCoordinator: UIScrollViewDelegate { let yOffset = scrollView.contentOffset.y if yOffset <= 0 { + isNavBarHidden = false updateNavBarVisibilityRatio(1) return } if yOffset < 30 { - updateNavBarVisibilityRatio(1) // yOffset / 30) + let isScrollingUp = yOffsetAtStartOfDrag ?? 0 > yOffset + updateNavBarVisibilityRatio(isScrollingUp ? 1 : 1 - (yOffset / 30)) return } guard let yOffsetAtStartOfDrag = yOffsetAtStartOfDrag else { return } - if yOffset > yOffsetAtStartOfDrag { + if yOffset > yOffsetAtStartOfDrag, !isNavBarHidden { let translation = yOffset - yOffsetAtStartOfDrag - let ratio = 0.0 // translation < 30 ? translation / 30 : 0 + let ratio = translation < 30 ? translation / 30 : 0 + isNavBarHidden = ratio == 0 updateNavBarVisibilityRatio(ratio) } } @@ -69,6 +73,7 @@ extension WebAppViewCoordinator: UIScrollViewDelegate { func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { if decelerate, scrollView.contentOffset.y < (yOffsetAtStartOfDrag ?? 0) { updateNavBarVisibilityRatio(1) + isNavBarHidden = false } yOffsetAtStartOfDrag = nil } diff --git a/apple/OmnivoreKit/Sources/Views/LinkedItemDetail/LinkItemDetailView.swift b/apple/OmnivoreKit/Sources/Views/LinkedItemDetail/LinkItemDetailView.swift index bb26f31f6..8c5a9d1f0 100644 --- a/apple/OmnivoreKit/Sources/Views/LinkedItemDetail/LinkItemDetailView.swift +++ b/apple/OmnivoreKit/Sources/Views/LinkedItemDetail/LinkItemDetailView.swift @@ -96,8 +96,9 @@ public struct LinkItemDetailView: View { } #endif } - .scaleEffect(x: 1, y: navBarVisibilityRatio) .frame(height: 30 * navBarVisibilityRatio) + .opacity(navBarVisibilityRatio) + .offset(x: 0.0, y: -30 * (1 - navBarVisibilityRatio)) } if let webAppWrapperViewModel = viewModel.webAppWrapperViewModel { WebAppWrapperView(