项目中有个人主页,顶部是个人信息,下面几个tab分别对应不同的帖子类型,和微博个人页很相似,效果如下:
实现思路:
最底部是一个左右滑动的UIScrollView,每个tab对应一个ContentViewController,将ContentViewController的view添加到ScrollView上;
lazy var bottomScrollView: UIScrollView = {
let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight))
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
scrollView.bounces = false
scrollView.contentSize = CGSize(width: ScreenWidth * 4, height: ScreenHeight)
scrollView.delegate = self
scrollView.isPagingEnabled = true
for i in 0...3 {
let contentVC = ContentViewController()
contentVC.view.frame = CGRect(x: ScreenWidth * CGFloat(i), y: 0, width: ScreenWidth, height: ScreenHeight)
if i == 0 {
contentVC.delegate = self
}
self.viewControllerArray.append(contentVC)
self.addChildViewController(contentVC)
scrollView.addSubview(contentVC.view)
}
return scrollView
}()
顶部展示的个人信息和切换tab,添加到ScrollView的上层;
lazy var headerView: UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: HeaderHeight))
view.backgroundColor = .red
return view
}()
lazy var topScrollView: TabScrollView = {
let scrollView = TabScrollView(frame: CGRect(x: 0, y: HeaderHeight, width: ScreenWidth, height: TopScrollHeight), nameArray: ["tab0","tab1","tab2","tab3"])
scrollView.delegate = self
scrollView.backgroundColor = .white
return scrollView
}()
view.addSubview(bottomScrollView)
view.addSubview(headerView)
view.addSubview(topScrollView)
顶部个人信息的View肯定会遮挡ContentViewController的内容,要调整ContentViewController内容的开始位置,由于这里使用UITableView,通过调整contentInset的方式,注意这里contentInset的top要和headerView+topScrollView的高度一致
lazy var listTableView: UITableView = {
let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight), style: .plain)
tableView.contentInset = UIEdgeInsetsMake(285, 0, 0, 0)
tableView.delegate = self
tableView.dataSource = self
tableView.backgroundColor = UIColorFromRGB(0xf5f3f0)
return tableView
}()
当ContentViewController中的tableview滑动时通过代理方法来改变顶部View的frame
//ContentViewController
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offsetY = scrollView.contentOffset.y
if self.delegate != nil && (self.delegate?.responds(to: #selector(ContentVCScrollDelegate.contentVCDidScrollContentOffset(_:))))! {
self.delegate?.contentVCDidScrollContentOffset(offsetY)
}
}
//Viewcontroller
func contentVCDidScrollContentOffset(_ offsetY: CGFloat) {
for contentVC in self.viewControllerArray {
let height: CGFloat = -45 - 64
if offsetY <= height {
contentVC.listTableView.contentOffset = CGPoint(x: 0, y: offsetY)
} else {
let positionY = contentVC.listTableView.contentOffset.y
if positionY < height {
//当某个tableview向上滑动露出headerview后,把所有的tableview滑动到顶部
contentVC.listTableView.contentOffset = CGPoint(x: 0, y: height)
}
}
}
let y = offsetY + HeaderHeight + TopScrollHeight
let alpha = Float(y/64.0)
topView.backgroundColor = UIColor(colorLiteralRed: 1.0, green: 1.0, blue: 1.0, alpha: alpha)
lineView.backgroundColor = UIColor(colorLiteralRed: 237.0/255.0, green: 231.0/255.0, blue: 228.0/255.0, alpha: alpha)
if y > 34 {
UIView.animate(withDuration: 0.4, animations: {
self.titleLabel.textColor = UIColorFromRGB(0xff7788)
UIApplication.shared.setStatusBarStyle(.default, animated: true)
})
} else {
UIView.animate(withDuration: 0.4, animations: {
self.titleLabel.textColor = .white
UIApplication.shared.setStatusBarStyle(.lightContent, animated: true)
})
}
if y >= HeaderHeight - 64 {
headerView.frame = CGRect(x: 0, y: -HeaderHeight + 64, width: ScreenWidth, height: HeaderHeight)
topScrollView.frame = CGRect(x: 0, y: 64, width: ScreenWidth, height: 45)
} else if y <= 0 {
headerView.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: -y + HeaderHeight)
topScrollView.frame = CGRect(x: 0, y: -y + HeaderHeight, width: ScreenWidth, height: 45)
} else {
headerView.frame = CGRect(x: 0, y: -y, width: ScreenWidth, height: HeaderHeight)
topScrollView.frame = CGRect(x: 0, y: -y + HeaderHeight, width: ScreenWidth, height: 45)
}
}
当tab切换或底部的UIScrollView左右滑动后要确保只有一个ContentViewController触发代理事件
func setDelegateWithOffset(_ offsetX: CGFloat) -> Void {
for contentVC in self.viewControllerArray {
contentVC.delegate = nil
}
let index = Int(offsetX)/Int(ScreenWidth)
if index < viewControllerArray.count {
let contentVC = viewControllerArray[index]
contentVC.delegate = self
}
}
附源码地址,如有不足之处,欢迎指正。