看過斗魚 APP 的首頁上滑隱藏導(dǎo)航,下滑顯示導(dǎo)航效果很流暢,使用效果非常不錯
原理:UItableView或 UIcollectionView 都是繼承UIScrollView 滑動的時候,判斷是上滑還是下滑 使用 UIScrollView 的代理方法
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let pan = scrollView.panGestureRecognizer
let velocity = pan.velocity(in: scrollView).y
if velocity < -15 {
//上滑
self.navigationController?.setNavigationBarHidden(true, animated: true)
//狀態(tài)欄顏色為黑色
UIApplication.shared.statusBarStyle = .default
NotificationCenter.default.post(name: NSNotification.Name(rawValue: kUpdateTitleFreamNote), object: nil)
} else if velocity > 15 {
//下滑
self.navigationController?.setNavigationBarHidden(false, animated: true)
//狀態(tài)欄顏色為白色
UIApplication.shared.statusBarStyle = .lightContent
NotificationCenter.default.post(name: NSNotification.Name(rawValue: kInUpdateTitleFreamNote), object: nil)
}
}
上滑時狀態(tài)欄顏色為黑色,導(dǎo)航隱藏,下滑導(dǎo)航欄顯示,狀態(tài)欄變?yōu)榘咨?/p>
至于控件的布局需要根據(jù)狀態(tài)去改變,
效果圖:

842039-20170509091517347-231642136.gif