添加左侧面板(汉堡菜单),导致黑屏

时间:2019-02-24 13:36:35

标签: ios swift uikit

这是我的代码:

import UIKit
import QuartzCore

enum slideOutState{
    case collapsed
    case leftPanelExpanded
}

enum ShowWhichVC {
    case homeVC
}

var showVC : ShowWhichVC = .homeVC

class ContainerVC: UIViewController {
    var currentState : slideOutState = .collapsed
    var homeVC : HomeVC!

    var leftVC : LeftSidePanelVC!
    //TODO: Declare temporary center view controller :

    var centerController : UIViewController!

    // TODO: Check if status bar is hidden :
    var isHiiden = false

    //TODO : How much do you want leftpanel to slide in :
    let centralPanelExpanded : CGFloat = 160

    var tap : UITapGestureRecognizer!

    override func viewDidLoad() {
        super.viewDidLoad()

        initCenter(screen: showVC)
    }

    func initCenter(screen: ShowWhichVC){
        var presentingController : UIViewController

        if homeVC == nil {
            homeVC = UIStoryboard.homeVC()
            homeVC.delegate = self

        }

        presentingController = homeVC

        if let con = centerController{
            con.view.removeFromSuperview()
            con.removeFromParent()
        }

        centerController = presentingController

        view.addSubview(centerController.view)
        addChild(centerController)
        centerController.didMove(toParent: self)
    }

    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation{
        return UIStatusBarAnimation.slide
    }

    override var prefersStatusBarHidden: Bool{
        return isHiiden
    }

}

extension ContainerVC : CenterVCDelegate {
    func toggleLeftPanel() {
        let notAlreadyExpanded = (currentState != .leftPanelExpanded)

        if notAlreadyExpanded {
            addLeftPanelViewController()
        }
        animateLeftPanel(shouldExpand: notAlreadyExpanded)
    }

    func addLeftPanelViewController() {
        if leftVC == nil {
            leftVC = UIStoryboard.leftViewController()
            addChildSidePanelViewController(sidePanelController: leftVC!)
        }
    }

    func addChildSidePanelViewController(sidePanelController: LeftSidePanelVC){
        view.insertSubview(sidePanelController.view, at: 1)
        addChild(sidePanelController)
        sidePanelController.didMove(toParent: self)
    }

    @objc func animateLeftPanel(shouldExpand: Bool) {
        if shouldExpand {
            isHiiden = !isHiiden
            animateStatusBar()

            setUpWhiteCoverView()

            currentState = .leftPanelExpanded

            animateCenterPanelXPosition(targetPosition: centerController.view.frame.width - centralPanelExpanded)
        }else {
            isHiiden = !isHiiden

            animateStatusBar()
            //setUpWhiteCoverView()
            hideWhiteCoverView()
            animateCenterPanelXPosition(targetPosition: 0) { (finished) in
                self.currentState = .collapsed
                self.leftVC = nil
            }
        }
    }

    func animateCenterPanelXPosition(targetPosition : CGFloat, completion: ((Bool) -> Void)! = nil ){
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
            self.view.frame.origin.x = targetPosition
        }, completion: completion)
    }

    func animateStatusBar(){
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
            self.setNeedsStatusBarAppearanceUpdate()
        })
    }

    func setUpWhiteCoverView(){
        let whiteCoverView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
        whiteCoverView.alpha = 0.0
        whiteCoverView.backgroundColor = UIColor.white
        whiteCoverView.tag = 25

        self.centerController.view.addSubview(whiteCoverView)
        UIView.animate(withDuration: 0.2) {
            whiteCoverView.alpha = 0.75
        }

        tap = UITapGestureRecognizer.init(target: self, action: #selector(animateLeftPanel(shouldExpand:)))
        tap.numberOfTapsRequired = 1

        self.centerController.view.addGestureRecognizer(tap)
    }

    func hideWhiteCoverView() {
        // Search for all the subview

        centerController.view.removeGestureRecognizer(tap)

        for subview in self.centerController.view.subviews{
            if subview.tag == 25 {
                UIView.animate(withDuration: 0.2, animations: {
                    subview.alpha = 0.0
                }) { (finished) in
                    subview.removeFromSuperview()
                }
            }
        }
    }
}

private extension UIStoryboard{
    class func mainStoryBoard() -> UIStoryboard{
        return UIStoryboard(name: "Main", bundle: Bundle.main)
    }

    class func leftViewController() -> LeftSidePanelVC?{
        return mainStoryBoard().instantiateViewController(withIdentifier: "LeftSidePanelVC") as? LeftSidePanelVC
    }

    class func homeVC() -> HomeVC?{
        return mainStoryBoard().instantiateViewController(withIdentifier: "HomeVC") as? HomeVC
    }
}

我不知道为什么,但是当我按下按钮时,它显示黑屏。

func addChildSidePanelViewController(sidePanelController: LeftSidePanelVC){
    view.insertSubview(sidePanelController.view, at: 1)
    addChild(sidePanelController)
    sidePanelController.didMove(toParent: self)
}

但是,当我将值1而不是0(sidePanelController.view,at:1)放到这里时,它向我显示了按钮,但在最右侧,屏幕的一半从左侧仍然是黑色。现在,通过尝试使用这部分代码,我试图找出问题出在哪里,在这里您可以看到屏幕截图:

值为0的屏幕截图:

Screen shot with value 0

屏幕快照的值为1:

screen shot with value 1

我什至尝试将0.5除以1仍然没有成功。

0 个答案:

没有答案