我有一个带有uiview头和表视图的父滚动视图。我试图有一个类似的行为推特或Instagram个人资料页面。开始滚动父滚动视图,头一离开,就会有一个连续从滚动视图滚动到表视图滚动,而不必提起手指并在表视图中替换它。目前的行为有点拘谨。我必须举起手指,在滚动视图内容偏移量通过标题后,将其放到表视图中,以开始滑动表视图。
MainViewController
import UIKit
import XLPagerTabStrip
class MainViewController: UIViewController {
lazy var headerViewController: UIViewController = {
let header = UIViewController()
return header
}()
lazy var bottomViewControllers: BottomPageViewController = {
let bvc = BottomPageViewController()
return bvc
}()
lazy var scrollView: UIScrollView = {
let sv = UIScrollView()
sv.delegate = self
sv.showsVerticalScrollIndicator = false
sv.bounces = true
sv.bounces = false
return sv
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(scrollView)
let f = UIScreen.main.bounds
scrollView.frame = CGRect(x: f.minX, y: f.minY, width: f.width, height: f.height)
add(headerViewController, to: scrollView, frame: CGRect(x: 0, y: 0, width: f.width, height: 150))
add(bottomViewControllers, to: scrollView, frame: CGRect(x: 0, y: headerViewController.view.bounds.height, width: f.width, height: f.height))
scrollView.contentSize = CGSize(width: f.width, height: f.height * 2)
if let vcs = bottomViewControllers.viewControllers as? [BottomViewController] {
for vc in vcs {
vc.delegate = self
}
}
}
}
extension MainViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard let controllers = bottomViewControllers.viewControllers as? [BottomViewController] else { return }
let selectedController = controllers[bottomViewControllers.currentIndex]
if self.scrollView == scrollView {
selectedController.tableView.isScrollEnabled = self.scrollView.contentOffset.y >= 150
print(scrollView.contentOffset.y)
scrollView.isScrollEnabled = !selectedController.tableView.isScrollEnabled
bottomViewControllers.view.frame.origin.y = max(150, scrollView.contentOffset.y)
}
}
}
extension MainViewController: CustomScrollDelegate {
func tableViewScroll(_ viewController: BottomViewController) {
print(viewController.tableView.contentOffset.y)
viewController.tableView.isScrollEnabled = viewController.tableView.contentOffset.y > 0
}
}
BottomViewController
import UIKit
import XLPagerTabStrip
protocol CustomScrollDelegate {
func tableViewScroll(_ viewController: BottomViewController)
}
class BottomViewController: UITableViewController {
var pageTitle: String?
var pageIndex: Int = 0
var delegate: CustomScrollDelegate?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .red
self.tableView.isUserInteractionEnabled = false
self.tableView.bounces = true
self.tableView.showsVerticalScrollIndicator = true
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "TETST")
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TETST", for: indexPath) as? UITableViewCell else { return UITableViewCell() }
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1000
}
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
delegate?.tableViewScroll(self)
}
init(pageTitle: String, pageIndex: Int) {
self.pageTitle = pageTitle
self.pageIndex = pageIndex
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension BottomViewController: IndicatorInfoProvider {
func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
return IndicatorInfo.init(title: pageTitle ?? "Tab \(pageIndex)")
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。