开发者社区> 问答> 正文

在tableView和scllView之间连续滚动

我有一个带有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)")
    }
}

展开
收起
游客5akardh5cojhg 2019-12-14 18:07:00 464 0
0 条回答
写回答
取消 提交回答
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载