Skip to content

Commit

Permalink
Fix adjust logic
Browse files Browse the repository at this point in the history
  • Loading branch information
horitaku46 committed Mar 14, 2018
1 parent 70661c6 commit e2a1fc0
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 37 deletions.
4 changes: 0 additions & 4 deletions Example/Sources/First/FirstViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ extension FirstViewController: UIScrollViewDelegate {
followNavigationBar()
}

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
decideNavigationBarState()
}

func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
showNavigationBar()
return true
Expand Down
4 changes: 0 additions & 4 deletions Example/Sources/Second/SecondViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ extension SecondViewController: UIScrollViewDelegate {
hideNavigationBar()
}

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
decideNavigationBarState()
}

func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
showNavigationBar()
return true
Expand Down
2 changes: 1 addition & 1 deletion SilentScrolly.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@
6154F5EA203B1CC600040F47 /* SilentScrolly */ = {
isa = PBXGroup;
children = (
615EC443205574EC00F2A392 /* SilentScrolly.swift */,
615EC444205574EC00F2A392 /* SilentScrollable.swift */,
615EC443205574EC00F2A392 /* SilentScrolly.swift */,
6154F5EB203B1CC600040F47 /* SilentScrolly.h */,
6154F5EC203B1CC600040F47 /* Info.plist */,
);
Expand Down
37 changes: 10 additions & 27 deletions SilentScrolly/SilentScrollable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,36 +116,23 @@ public extension SilentScrollable where Self: UIViewController {
return
}

let velocityY = scrollView.panGestureRecognizer.velocity(in: view).y
let positiveContentOffsetY = calcPositiveContentOffsetY(scrollView)

if positiveContentOffsetY != prevPositiveContentOffsetY && scrollView.isTracking {
isScrollUp(velocityY) ? adjustEitherView(scrollView, isShow: false) : adjustEitherView(scrollView, isShow: true)
}

silentScrolly?.prevPositiveContentOffsetY = positiveContentOffsetY
}

public func decideNavigationBarState() {
guard let scrollView = silentScrolly?.scrollView,
let showNavigationBarFrameOriginY = silentScrolly?.showNavigationBarFrameOriginY,
let currentNavigationBarOriginY = navigationController?.navigationBar.frame.origin.y else {
return
}

if scrollView.contentOffset.y.isZero {
if scrollView.contentOffset.y <= 0 {
adjustEitherView(scrollView, isShow: true)
return
}

let positiveContentOffsetY = calcPositiveContentOffsetY(scrollView)
let velocityY = scrollView.panGestureRecognizer.velocity(in: view).y
let navigationBarMoveDistance = fabs(currentNavigationBarOriginY - showNavigationBarFrameOriginY)

if velocityY < SilentScrolly.Const.maxFluctuateNavigationBarVelocityY {
navigationBarMoveDistance > 0 ? adjustEitherView(scrollView, isShow: false) : adjustEitherView(scrollView, isShow: true)
} else {
isScrollUp(velocityY) ? adjustEitherView(scrollView, isShow: false) : adjustEitherView(scrollView, isShow: true)
if positiveContentOffsetY != prevPositiveContentOffsetY && scrollView.isTracking {
if velocityY < SilentScrolly.Const.minDoNothingAdjustNavigationBarVelocityY {
adjustEitherView(scrollView, isShow: false)
} else if velocityY > SilentScrolly.Const.maxDoNothingAdjustNavigationBarVelocityY {
adjustEitherView(scrollView, isShow: true)
}
}

silentScrolly?.prevPositiveContentOffsetY = positiveContentOffsetY
}

public func showNavigationBar() {
Expand All @@ -168,10 +155,6 @@ public extension SilentScrollable where Self: UIViewController {
return contentOffsetY
}

private func isScrollUp(_ velocityY: CGFloat) -> Bool {
return velocityY <= 0
}

private func adjustEitherView(_ scrollView: UIScrollView, isShow: Bool, animated: Bool = true, completion: (() -> Void)? = nil) {
guard let isNavigationbarAnimateCompleted = silentScrolly?.isNavigationbarAnimateCompleted,
let showNavigationBarFrameOriginY = silentScrolly?.showNavigationBarFrameOriginY,
Expand Down
3 changes: 2 additions & 1 deletion SilentScrolly/SilentScrolly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import UIKit
public struct SilentScrolly {

public enum Const {
public static let maxFluctuateNavigationBarVelocityY: CGFloat = 100
public static let minDoNothingAdjustNavigationBarVelocityY: CGFloat = 0
public static let maxDoNothingAdjustNavigationBarVelocityY: CGFloat = 1400
public static let animateDuration: TimeInterval = 0.3
}

Expand Down

0 comments on commit e2a1fc0

Please sign in to comment.