看過斗魚 APP 的首頁上滑隱藏導航,下滑顯示導航效果很流暢,使用效果非常不錯
原理: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)
//狀態欄顏色為黑色
UIApplication.shared.statusBarStyle = .default
NotificationCenter.default.post(name: NSNotification.Name(rawValue: kUpdateTitleFreamNote), object: nil)
} else if velocity > 15 {
//下滑
self.navigationController?.setNavigationBarHidden(false, animated: true)
//狀態欄顏色為白色
UIApplication.shared.statusBarStyle = .lightContent
NotificationCenter.default.post(name: NSNotification.Name(rawValue: kInUpdateTitleFreamNote), object: nil)
}
}
上滑時狀態欄顏色為黑色,導航隱藏,下滑導航欄顯示,狀態欄變為白色
至于控件的布局需要根據狀態去改變,
效果圖:
842039-20170509091517347-231642136.gif