iOS-吸頂分頁控制器

HoverPageViewController

oc版本:https://github.com/QiaokeZ/iOS_HoverPageViewController/tree/master/HoverDome_OC

swift版本:https://github.com/QiaokeZ/iOS_HoverPageViewController/tree/master/HoverDome_Swift

hover.gif

主控制器

class ViewController: UIViewController {

    /// 可根據業(yè)務需求更改
    static let headerViewHeight: CGFloat = 200
    static let pageTitleViewHeight: CGFloat = 40
    
    var hoverPageViewController:HoverPageViewController!
    let indicator = UIView()
    var indicatorMargin:CGFloat = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.navigationBar.isTranslucent = false
        view.backgroundColor = UIColor.white
        title = "懸停"
        prepareView()
    }
}

extension ViewController {

    private func prepareView() {
        let headerView = UILabel()
        headerView.frame.size = CGSize(width: view.frame.width, height: ViewController.headerViewHeight)
        headerView.backgroundColor = UIColor.red
        headerView.text = "我是頭部"
        headerView.textAlignment = .center
        
        /// 指示器
        let pageTitleView = UIView()
        pageTitleView.frame.size = CGSize(width: view.frame.width, height: ViewController.pageTitleViewHeight)

        /// 添加3個按鈕
        let buttonSize = CGSize(width: view.frame.width / 3, height: pageTitleView.frame.height)
        for i in 0..<3 {
            let button = UIButton()
            button.tag = i
            button.frame.size = buttonSize
            button.backgroundColor = UIColor.lightGray
            button.frame.origin.x = CGFloat(i) * buttonSize.width
            button.setTitleColor(UIColor.black, for: .normal)
            button.setTitle("控制器", for: .normal)
            button.addTarget(self, action: #selector(ViewController.buttonClick), for: .touchUpInside)
            pageTitleView.addSubview(button)
        }
        
        let button = pageTitleView.subviews[0] as! UIButton
        button.layoutIfNeeded()
        indicator.frame.size = CGSize(width: (button.titleLabel?.frame.width)!, height: 3)
        indicator.backgroundColor = UIColor.yellow
        indicator.center.x = button.center.x
        indicator.frame.origin.y = button.frame.height - indicator.frame.height
        indicatorMargin = indicator.frame.origin.x
        pageTitleView.addSubview(indicator)
        
        /// 添加子控制器
        var viewControllers = [HoverContainerViewController]()
        let vc1 = Children1ViewController()
        let vc2 = Children2ViewController()
        let vc3 = Children3ViewController()
        viewControllers.append(vc1)
        viewControllers.append(vc2)
        viewControllers.append(vc3)
        
         
        hoverPageViewController = HoverPageViewController(viewControllers: viewControllers, headerView: headerView, pageTitleView: pageTitleView)
        hoverPageViewController.delegate = self
        addChild(hoverPageViewController)
        view.addSubview(hoverPageViewController.view)
    }

    @objc func buttonClick(btn: UIButton) {
        hoverPageViewController.selectedIndex = btn.tag
    }
}

extension ViewController:HoverPageViewControllerDelegate{
    
    func hoverPageViewController(_ viewController: HoverPageViewController, scrollViewDidScroll: UIScrollView) {
        let progress = scrollViewDidScroll.contentOffset.x / scrollViewDidScroll.frame.width
        indicator.frame.origin.x = ((indicator.frame.width + (indicatorMargin * 2)) * progress) + indicatorMargin
    }
}

子控制器

class Children1ViewController: HoverContainerViewController {

    private lazy var items: [String] = {
        var items = [String]()
        for i in 0..<100 {
            items.append("\(i)")
        }
        return items
    }()

    private lazy var tableView: UITableView = {
        let inset = UIEdgeInsets(top: ViewController.headerViewHeight, left: 0, bottom: 0, right: 0)
        let tableView = UITableView(frame: view.bounds, style: .plain)
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "aaaa")
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.bounces = false
        tableView.contentInset = UIEdgeInsets(top: ViewController.headerViewHeight, left: 0, bottom: 0, right: 0)
        tableView.scrollIndicatorInsets = tableView.contentInset
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        offsetY = -ViewController.headerViewHeight
        view.backgroundColor = UIColor.green
        view.addSubview(tableView)
    }

    override var offsetY: CGFloat {
        didSet {
            tableView.contentOffset = CGPoint(x: 0, y: offsetY)
        }
    }

    override var isStopScroll: Bool {
        didSet {
            if isStopScroll == true{
                tableView.setContentOffset(CGPoint(x: 0, y: tableView.contentOffset.y), animated: false)
            }
        }
    }
}

extension Children1ViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "aaaa", for: indexPath)
        cell.textLabel?.text = items[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: HoverPageViewController.HoverPageViewOffsetChange),
                                        object: self,
                                        userInfo: [HoverPageViewController.OffsetKey: scrollView.contentOffset])
        offsetY = scrollView.contentOffset.y
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 正位能量名是這樣的[微笑] 相遇,本是前世約定; 相聚,源于靈魂感應。 感恩生命中的遇見 感恩微信名叫“妹寶”的家...
    正位能量名閱讀 1,904評論 0 1
  • 小時候,總是盼著農歷新年,因為可以在假期愉快地玩耍,穿新衣戴新帽,吃各種美食,親戚還會給壓歲錢。 每年的臘月二十至...
    陽_陽陽閱讀 292評論 0 0
  • 上文末尾講述了一個有趣的情況:由于撕逼對象堅持為了做而做,我們敬愛的產品童鞋禮貌的說: 現在,讓我們復盤下過程,如...
    初社TryShr閱讀 906評論 3 27
  • 都這個時候了,你還在穿上像一只小豬豬呼呼大睡,而我在一旁靜靜地看著你,忍不住該為你拍了幾張照片。而你在翻身的時候卻...
    豌豆的媽媽閱讀 339評論 0 2
  • 上午請假沒去上班,下午剛進辦公室就接到一個陌生電話?!袄蠋?,我是陸濤,上午去給你送請柬你不在,26號你可一定來啊…...
    豫妞萍姐閱讀 552評論 2 3

友情鏈接更多精彩內容