Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6504cd8

Browse files
优化移动导航栏demo2滑动效果
1 parent 6556947 commit 6504cd8

File tree

4 files changed

+46
-22
lines changed

4 files changed

+46
-22
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ statusBarStyle = .default
7070
解决问题:解决scrollView正在滑动的时候,点击返回按钮,导航栏颜色变化突兀的问题
7171

7272
- **2017年06月19日**
73-
解决问题:解决移动导航栏后右滑返回中途取消的bug
73+
解决问题:解决移动导航栏后右滑返回中途取消导致的导航栏错位的问题
7474

7575

7676
## Features

‎WRNavigationBar/WRNavigationBar.swift‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ extension UINavigationBar
9999
{
100100
transform = CGAffineTransform.init(translationX: 0, y: translationY)
101101
}
102+
103+
func wr_getTranslationY() -> CGFloat
104+
{
105+
return transform.ty
106+
}
102107
}
103108

104109
//==========================================================================
@@ -231,7 +236,6 @@ extension UINavigationController
231236
// change navigationBar barTintColor smooth before pop to current VC finished
232237
func popNeedDisplay()
233238
{
234-
print("popNeedDisplay")
235239
guard let topViewController = topViewController,
236240
let coordinator = topViewController.transitionCoordinator else {
237241
return

‎WRNavigationBar_swift/WRNavigationBar_swift/Demos/ThirdViewController.swift‎

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99

1010
import UIKit
1111

12-
private let NAVBAR_TRANSLATION_POINT:CGFloat = 0
1312

1413
class ThirdViewController: UIViewController
1514
{
15+
fileprivate var NAVBAR_TRANSLATION_POINT:CGFloat = 0
16+
fileprivate var lastOffsetY:CGFloat = 0
17+
1618
lazy var tableView:UITableView = {
1719
let table:UITableView = UITableView(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: self.view.bounds.height), style: .plain)
1820
table.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
@@ -78,48 +80,62 @@ extension ThirdViewController
7880
{ // 超过导航栏高度的一半,只显示状态栏
7981
UIView.animate(withDuration: 0.3, animations: { [weak self] in
8082
if let weakSelf = self {
81-
weakSelf.setNavigationBarTransformProgress(progress:1)
83+
weakSelf.setNavigationBarTransform(scrollUpHeight:44)
8284
}
8385
})
86+
print("point:\(NAVBAR_TRANSLATION_POINT)")
8487
}
8588
else
8689
{ // 没有超过导航栏高度的一半,导航栏全部显示
8790
UIView.animate(withDuration: 0.3, animations: { [weak self] in
8891
if let weakSelf = self {
89-
weakSelf.setNavigationBarTransformProgress(progress: 0)
92+
weakSelf.setNavigationBarTransform(scrollUpHeight: 0)
9093
}
9194
})
95+
print("point:\(NAVBAR_TRANSLATION_POINT)")
9296
}
97+
NAVBAR_TRANSLATION_POINT = offsetY
9398
}
9499

95100
func scrollViewDidScroll(_ scrollView: UIScrollView)
96101
{
97102
let offsetY = scrollView.contentOffset.y
98103
// 向上滑动的距离
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
106113
}
107-
else {
108-
setNavigationBarTransformProgress(progress: progress)
114+
else
115+
{
116+
if offsetY > 0 {
117+
setNavigationBarTransform(scrollUpHeight: scrollUpHeight)
118+
}
109119
}
110120
}
111121
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+
})
114128
}
129+
130+
lastOffsetY = offsetY
115131
}
116-
117-
// private
118-
func setNavigationBarTransformProgress(progress:CGFloat)
132+
133+
func setNavigationBarTransform(scrollUpHeight:CGFloat)
119134
{
120-
navigationController?.navigationBar.wr_setTranslationY(translationY: -CGFloat(kNavBarHeight)* progress)
135+
navigationController?.navigationBar.wr_setTranslationY(translationY: -scrollUpHeight)
121136
// 有系统的返回按钮,所以 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)
123139
}
124140
}
125141

‎WRNavigationBar_swift/WRNavigationBar_swift/WRNavigationBar/WRNavigationBar.swift‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ extension UINavigationBar
9999
{
100100
transform = CGAffineTransform.init(translationX: 0, y: translationY)
101101
}
102+
103+
func wr_getTranslationY() -> CGFloat
104+
{
105+
return transform.ty
106+
}
102107
}
103108

104109
//==========================================================================
@@ -231,7 +236,6 @@ extension UINavigationController
231236
// change navigationBar barTintColor smooth before pop to current VC finished
232237
func popNeedDisplay()
233238
{
234-
print("popNeedDisplay")
235239
guard let topViewController = topViewController,
236240
let coordinator = topViewController.transitionCoordinator else {
237241
return

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /