如何使用分段控件(如instagram个人资料在两个重叠的容器视图之间添加滑动手势或滑动功能?

问题描述 投票:0回答:1

我是这个iOS开发领域的新手,正在尝试为我的学习构建一个应用程序。我被困在申请的最后阶段。我想使用分段控件在两个容器视图之间滑动,我想在单个控制器(如instagram个人资料)的半屏上添加两个大小相同的重叠容器视图(带有带有子控制器的嵌入segue的容器视图),在容器视图的两个子控制器上添加tableview,当我运行该应用程序时,将看到课程的第一个子视图的数据,但是当我向左滑动时,将以相同的大小和相同的方式看到第二个子控制器就像我们的instagram个人资料一样,第一个孩子控制者和第一个孩子控制者的位置应该被隐藏,并且根据我的工作,当我向左滑动时,第二个孩子控制者会全屏打开并在该屏幕上隐藏其他内容我已经在许多教程的帮助下尝试过此滑动功能无法正常工作,但未成功。请告诉我该怎么做?谢谢 !

swift uitableview uisegmentedcontrol uicontainerview uiswipegesturerecognizer
1个回答
0
投票

这是您想要的简单示例。这是全屏设计。我想您可以找到一个想法,并根据您的要求对其进行自定义

class ContainerController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        if #available(iOS 13.0, *) {
            view.backgroundColor = .systemBackground
        } else {
            view.backgroundColor = .white
        }

        navigationItem.titleView = sgControll
        updateView()

        sgControll.addTarget(self, action: #selector(segmentControllValueChanged (_:)), for:.valueChanged)
    }

    //MARK: Components
    let sgControll:UISegmentedControl = {
        let sg = UISegmentedControl()
        sg.insertSegment(withTitle: "first Child", at: 0, animated: true)
        sg.insertSegment(withTitle: "Second Child", at: 1, animated: true)
        sg.selectedSegmentIndex = 0
        return sg
    }()

    private lazy var secondView : SecondChildViewController = {
        let vc = SecondChildViewController()
        self.add(asChildViewController: vc)
        return vc
    }()

    private lazy var firstView:FirstChildViewController = {
        let vc = FirstChildViewwController()
        self.add(asChildViewController: vc)
        return vc
    }()

}


//MARK: Functions
extension ContainerController {

    @objc func segmentControllValueChanged(_ sender : UISegmentedControl){
        print(sender.selectedSegmentIndex)
        updateView()
    }

    private func add(asChildViewController viewController: UIViewController){
        addChild(viewController)
        view.addSubview(viewController.view)
        viewController.view.translatesAutoresizingMaskIntoConstraints = false
        viewController.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        viewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        viewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        viewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        viewController.didMove(toParent: self)
    }

    private func remove(asChildViewController viewController: UIViewController) {
        viewController.willMove(toParent: nil)
        viewController.view.removeFromSuperview()
        viewController.removeFromParent()
    }

private func updateView() {
        if sgControll.selectedSegmentIndex == 0 {
            remove(asChildViewController: firstView)
            add(asChildViewController: secondView)
        } else {
            remove(asChildViewController: seconView)
            add(asChildViewController: firstView)
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.