|
9 | 9 |
|
10 | 10 | import UIKit
|
11 | 11 |
|
12 |
| -private let NAVBAR_TRANSLATION_POINT:CGFloat = 0 |
13 | 12 |
|
14 | 13 | class ThirdViewController: UIViewController
|
15 | 14 | {
|
| 15 | + fileprivate var NAVBAR_TRANSLATION_POINT:CGFloat = 0 |
| 16 | + fileprivate var lastOffsetY:CGFloat = 0 |
| 17 | + |
16 | 18 | lazy var tableView:UITableView = {
|
17 | 19 | let table:UITableView = UITableView(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: self.view.bounds.height), style: .plain)
|
18 | 20 | table.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
|
@@ -78,48 +80,62 @@ extension ThirdViewController
|
78 | 80 | { // 超过导航栏高度的一半,只显示状态栏
|
79 | 81 | UIView.animate(withDuration: 0.3, animations: { [weak self] in
|
80 | 82 | if let weakSelf = self {
|
81 |
| - weakSelf.setNavigationBarTransformProgress(progress: 1) |
| 83 | + weakSelf.setNavigationBarTransform(scrollUpHeight: 44) |
82 | 84 | }
|
83 | 85 | })
|
| 86 | + print("point:\(NAVBAR_TRANSLATION_POINT)") |
84 | 87 | }
|
85 | 88 | else
|
86 | 89 | { // 没有超过导航栏高度的一半,导航栏全部显示
|
87 | 90 | UIView.animate(withDuration: 0.3, animations: { [weak self] in
|
88 | 91 | if let weakSelf = self {
|
89 |
| - weakSelf.setNavigationBarTransformProgress(progress: 0) |
| 92 | + weakSelf.setNavigationBarTransform(scrollUpHeight: 0) |
90 | 93 | }
|
91 | 94 | })
|
| 95 | + print("point:\(NAVBAR_TRANSLATION_POINT)") |
92 | 96 | }
|
| 97 | + NAVBAR_TRANSLATION_POINT = offsetY |
93 | 98 | }
|
94 | 99 |
|
95 | 100 | func scrollViewDidScroll(_ scrollView: UIScrollView)
|
96 | 101 | {
|
97 | 102 | let offsetY = scrollView.contentOffset.y
|
98 | 103 | // 向上滑动的距离
|
99 |
| - let scrollUpHeight = offsetY - NAVBAR_TRANSLATION_POINT |
100 |
| - // 除数表示 -> 导航栏从完全不透明到完全透明的过渡距离 |
101 |
| - let progress = scrollUpHeight / CGFloat(kNavBarHeight) |
102 |
| - if (offsetY > NAVBAR_TRANSLATION_POINT) |
103 |
| - { |
104 |
| - if (scrollUpHeight > CGFloat(kNavBarHeight)) { |
105 |
| - setNavigationBarTransformProgress(progress: 1) |
| 104 | + let isScrollup = (offsetY - lastOffsetY) > 0 ? true : false |
| 105 | + let scrollUpHeight = (offsetY - NAVBAR_TRANSLATION_POINT) > 44 ? 44 : (offsetY - NAVBAR_TRANSLATION_POINT) |
| 106 | + let curTransformY = navigationController?.navigationBar.wr_getTranslationY() |
| 107 | + |
| 108 | + |
| 109 | + if isScrollup == true |
| 110 | + { // 上滑 |
| 111 | + if curTransformY == -44 { |
| 112 | + return |
106 | 113 | }
|
107 |
| - else { |
108 |
| - setNavigationBarTransformProgress(progress: progress) |
| 114 | + else |
| 115 | + { |
| 116 | + if offsetY > 0 { |
| 117 | + setNavigationBarTransform(scrollUpHeight: scrollUpHeight) |
| 118 | + } |
109 | 119 | }
|
110 | 120 | }
|
111 | 121 | else
|
112 |
| - { |
113 |
| - self.setNavigationBarTransformProgress(progress: 0) |
| 122 | + { // 下滑 |
| 123 | + UIView.animate(withDuration: 0.3, animations: { [weak self] in |
| 124 | + if let weakSelf = self { |
| 125 | + weakSelf.setNavigationBarTransform(scrollUpHeight: 0) |
| 126 | + } |
| 127 | + }) |
114 | 128 | }
|
| 129 | + |
| 130 | + lastOffsetY = offsetY |
115 | 131 | }
|
116 |
| - |
117 |
| - // private |
118 |
| - func setNavigationBarTransformProgress(progress:CGFloat) |
| 132 | + |
| 133 | + func setNavigationBarTransform(scrollUpHeight:CGFloat) |
119 | 134 | {
|
120 |
| - navigationController?.navigationBar.wr_setTranslationY(translationY: -CGFloat(kNavBarHeight) * progress) |
| 135 | + navigationController?.navigationBar.wr_setTranslationY(translationY: -scrollUpHeight) |
121 | 136 | // 有系统的返回按钮,所以 hasSystemBackIndicator = YES
|
122 |
| - navigationController?.navigationBar.wr_setBarButtonItemsAlpha(alpha: 1 - progress, hasSystemBackIndicator: true) |
| 137 | + let curTransformY = navigationController?.navigationBar.wr_getTranslationY() ?? 0 |
| 138 | + navigationController?.navigationBar.wr_setBarButtonItemsAlpha(alpha: 1 - (-curTransformY / 44.0), hasSystemBackIndicator: true) |
123 | 139 | }
|
124 | 140 | }
|
125 | 141 |
|
|
0 commit comments