-
Notifications
You must be signed in to change notification settings - Fork 10
/
NavBarHairlineFadeBehavior.swift
164 lines (125 loc) · 4.84 KB
/
NavBarHairlineFadeBehavior.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// NavBarHairlineFadeBehavior.swift
// Swiftilities
//
// Created by Jason Clark on 5/12/17.
//
//
#if canImport(UIKit)
import UIKit
public final class NavBarHairlineFadeBehavior: ViewControllerLifecycleBehavior {
fileprivate var navBarHairlineFadeUpdater: NavBarHairlineFadeUpdater
public init(scrollView: UIScrollView) {
self.navBarHairlineFadeUpdater = NavBarHairlineFadeUpdater(scrollView: scrollView)
self.navBarHairlineFadeUpdater.contentOffsetFadeRange = contentOffsetFadeRange
}
public func beforeAppearing(_ viewController: UIViewController, animated: Bool) {
navBarHairlineFadeUpdater.navigationBar = viewController.navigationController?.navigationBar
navBarHairlineFadeUpdater.enabled = true
}
public func beforeDisappearing(_ viewController: UIViewController, animated: Bool) {
navBarHairlineFadeUpdater.enabled = false
}
deinit {
navBarHairlineFadeUpdater.enabled = false
}
}
public extension NavBarHairlineFadeBehavior {
var contentOffsetFadeRange: ClosedRange<CGFloat> {
set { navBarHairlineFadeUpdater.contentOffsetFadeRange = newValue }
get { return navBarHairlineFadeUpdater.contentOffsetFadeRange }
}
var hairlineColor: UIColor {
set { navBarHairlineFadeUpdater.hairline.hairlineColor = newValue }
get { return navBarHairlineFadeUpdater.hairline.hairlineColor }
}
var hairlineThickness: CGFloat {
set { navBarHairlineFadeUpdater.hairline.thickness = newValue }
get { return navBarHairlineFadeUpdater.hairline.thickness }
}
/// Toggle for debug purposes:
/// Logs the scrollview's y-offset while scrolling which helps implementer tweak the fade range more easily.
var logsContentOffset: Bool {
set { navBarHairlineFadeUpdater.logsContentOffset = newValue }
get { return navBarHairlineFadeUpdater.logsContentOffset }
}
}
fileprivate final class NavBarHairlineFadeUpdater: NSObject {
var contentOffsetFadeRange: ClosedRange<CGFloat> = 0...100
var logsContentOffset: Bool = false
var enabled: Bool = false {
didSet {
if enabled != oldValue {
enabled ? activate() : cleanUp()
}
}
}
var hairlineAlpha: CGFloat {
set { hairline.alpha = newValue }
get { return hairline.alpha }
}
let hairline = HairlineView(axis: .horizontal)
weak var scrollView: UIScrollView?
weak var navigationBar: UINavigationBar?
init(scrollView: UIScrollView) {
self.scrollView = scrollView
super.init()
self.hairlineAlpha = 0
}
func activate() {
addObservers()
if let navBar = navigationBar {
navBar.addSubview(hairline)
navBarDidChangeBounds(navBar)
}
}
func cleanUp() {
removeObservers()
hairline.removeFromSuperview()
}
deinit {
enabled = false
}
}
fileprivate extension NavBarHairlineFadeUpdater {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let shift = scrollView.contentInset.top + scrollView.contentOffset.y
hairlineAlpha = shift.scaled(from: contentOffsetFadeRange, to: 0...1, clamped: true)
#if DEBUG
if logsContentOffset {
print("\(NavBarHairlineFadeUpdater.self) content y-offset: \(String(format: "%0.3f", shift))")
}
#endif
}
func navBarDidChangeBounds(_ navBar: UINavigationBar) {
let rect = navBar.bounds
let thickness = hairline.intrinsicContentSize.height
hairline.frame = CGRect(x: 0, y: rect.height - thickness, width: rect.width, height: thickness)
}
}
fileprivate extension NavBarHairlineFadeUpdater {
enum KeyPath {
static let contentOffset = #keyPath(UIScrollView.contentOffset)
static let navBarBounds = #keyPath(UINavigationBar.bounds)
}
func addObservers() {
self.navigationBar?.addObserver(self, forKeyPath: KeyPath.navBarBounds, options: .new, context: nil)
self.scrollView?.addObserver(self, forKeyPath: KeyPath.contentOffset, options: .new, context: nil)
}
func removeObservers() {
scrollView?.removeObserver(self, forKeyPath: KeyPath.contentOffset)
navigationBar?.removeObserver(self, forKeyPath: KeyPath.navBarBounds)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == KeyPath.contentOffset, let sv = object as? UIScrollView {
scrollViewDidScroll(sv)
}
else if keyPath == KeyPath.navBarBounds, let nb = object as? UINavigationBar {
navBarDidChangeBounds(nb)
}
else {
super.observeValue(forKeyPath: keyPath, of:object, change: change, context: context)
}
}
}
#endif