Skip to content

Commit

Permalink
Fix show and hide handling
Browse files Browse the repository at this point in the history
  • Loading branch information
horitaku46 committed Mar 19, 2018
1 parent a3e05d0 commit 0f4cf8e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
14 changes: 14 additions & 0 deletions Example/Sources/First/FirstViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ final class FirstViewController: UIViewController, SilentScrollable {
configureSilentScrolly(webView.scrollView, followBottomView: tabBarController?.tabBar)
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationBarWillDisappear()
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
navigationBarDidDisappear()
}

@objc private func tapRightShowBarButtonItem() {
let viewController = SecondViewController.make()
navigationController?.show(viewController, sender: nil)
Expand All @@ -60,6 +70,10 @@ extension FirstViewController: UIScrollViewDelegate {
followNavigationBar()
}

func scrollViewDidZoom(_ scrollView: UIScrollView) {
hideNavigationBar()
}

func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
showNavigationBar()
return true
Expand Down
7 changes: 6 additions & 1 deletion Example/Sources/Second/SecondViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ final class SecondViewController: UIViewController, SilentScrollable {

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
showNavigationBar()
navigationBarWillDisappear()
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
navigationBarDidDisappear()
}
}

Expand Down
21 changes: 20 additions & 1 deletion SilentScrolly/SilentScrollable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public extension SilentScrollable where Self: UIViewController {

silentScrolly?.scrollView = scrollView

silentScrolly?.isNavigationBarShow = true
silentScrolly?.isNavigationbarAnimateCompleted = true
silentScrolly?.isTransitionCompleted = true

silentScrolly?.showNavigationBarFrameOriginY = statusBarHeight
silentScrolly?.hideNavigationBarFrameOriginY = -navigationBarHeight
silentScrolly?.showScrollIndicatorInsetsTop = scrollView.scrollIndicatorInsets.top
Expand Down Expand Up @@ -149,6 +153,20 @@ public extension SilentScrollable where Self: UIViewController {
adjustEitherView(scrollView, isShow: false)
}

public func navigationBarWill() {
showNavigationBar()
silentScrolly?.isTransitionCompleted = false
}

public func navigationBarWillDisappear() {
showNavigationBar()
silentScrolly?.isTransitionCompleted = false
}

public func navigationBarDidDisappear() {
silentScrolly?.isTransitionCompleted = true
}

private func calcPositiveContentOffsetY(_ scrollView: UIScrollView) -> CGFloat {
var contentOffsetY = scrollView.contentOffset.y + scrollView.contentInset.top
contentOffsetY = contentOffsetY > 0 ? contentOffsetY : 0
Expand All @@ -157,6 +175,7 @@ public extension SilentScrollable where Self: UIViewController {

private func adjustEitherView(_ scrollView: UIScrollView, isShow: Bool, animated: Bool = true, completion: (() -> Void)? = nil) {
guard let isNavigationbarAnimateCompleted = silentScrolly?.isNavigationbarAnimateCompleted,
let isTransitionCompleted = silentScrolly?.isTransitionCompleted,
let showNavigationBarFrameOriginY = silentScrolly?.showNavigationBarFrameOriginY,
let hideNavigationBarFrameOriginY = silentScrolly?.hideNavigationBarFrameOriginY,
let showScrollIndicatorInsetsTop = silentScrolly?.showScrollIndicatorInsetsTop,
Expand All @@ -165,7 +184,7 @@ public extension SilentScrollable where Self: UIViewController {
return
}

if scrollView.contentSize.height < scrollView.bounds.height || !isNavigationbarAnimateCompleted {
if scrollView.contentSize.height < scrollView.bounds.height || !isNavigationbarAnimateCompleted || !isTransitionCompleted {
return
}

Expand Down
1 change: 1 addition & 0 deletions SilentScrolly/SilentScrolly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public struct SilentScrolly {

public var isNavigationBarShow = true
public var isNavigationbarAnimateCompleted = true
public var isTransitionCompleted = true

public var prevPositiveContentOffsetY: CGFloat = 0

Expand Down

0 comments on commit 0f4cf8e

Please sign in to comment.