无法从sideMenu迅速删除启动器

时间:2018-06-25 04:15:07

标签: swift side-menu

我的应用看起来像这样enter image description here

然后我单击菜单,如下所示,然后单击测试 enter image description here

然后单击关闭enter image description here

按钮

我正在寻找删除上一个uiview的方法。请帮忙。

这是我的代码

import UIKit

class TabBarCtr: UITabBarController{

    var sidebarView: SidebarView!
    var blackScreen: UIView!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        view.backgroundColor = .white

//这里的东西

        sidebarView = SidebarView(frame: CGRect(x: 0, y: 0, width: 0, height: self.view.frame.height))
        sidebarView.delegate = self
    sidebarView.layer.zPosition = 100
    self.view.isUserInteractionEnabled = true
    view.addSubview(sidebarView)

    blackScreen=UIView(frame: self.view.bounds)
    blackScreen.backgroundColor=UIColor(white: 0, alpha: 0.5)
    blackScreen.isHidden = true
    view.addSubview(blackScreen)
    blackScreen.layer.zPosition=99
    let tapGestRecognizer = UITapGestureRecognizer(target: self, action: #selector(blackScreenTapAction(sender:)))
    blackScreen.addGestureRecognizer(tapGestRecognizer)

}

override func viewDidLoad() {
    super.viewDidLoad()

    viewControllers = [
         CreateDumyNavControllerWithTitle(title: "Home", imageName: "bell")
    ]
}

private func CreateDumyNavControllerWithTitle(title:String, imageName:String, collection:Bool = false)-> UINavigationController {

    let viewCtr = HomeCtrViewController()
    viewCtr.navigationItem.leftBarButtonItem = setupLeftNavBarButtons()

    let navCtr = UINavigationController(rootViewController: viewCtr)
    navCtr.hidesBarsOnSwipe = true
    navCtr.tabBarItem.title = title
    navCtr.tabBarItem.image = UIImage(named: imageName)

    return navCtr
}

func setupLeftNavBarButtons()-> UIBarButtonItem{

    let settingsImage = UIImage(named: "menu-2")?.withRenderingMode(.automatic)
    let settingBarButtonItem = UIBarButtonItem(image: settingsImage, style: .plain, target: self, action: #selector(btnMenuAction))

    return settingBarButtonItem
}

@objc func btnMenuAction() {

    blackScreen.isHidden = false
    UIView.animate(withDuration: 0.3, animations: {
        self.sidebarView.frame=CGRect(x: 0, y: 0, width: 250, height: self.sidebarView.frame.height)
    }) { (complete) in
        self.blackScreen.frame=CGRect(x: self.sidebarView.frame.width, y: 0, width: self.view.frame.width-self.sidebarView.frame.width, height: self.view.bounds.height+100)
    }
}

@objc func blackScreenTapAction(sender: UITapGestureRecognizer) {
    blackScreen.isHidden=true
    blackScreen.frame=self.view.bounds
    UIView.animate(withDuration: 0.3) {
        self.sidebarView.frame=CGRect(x: 0, y: 0, width: 0, height: self.sidebarView.frame.height)
        }
    }
}



// sideMenu


class SidebarView: UIView, UITableViewDelegate, UITableViewDataSource {

var titleArr = [String]()
weak var delegate: SidebarViewDelegate?

override init(frame: CGRect) {
    super.init(frame: frame)
    self.backgroundColor=UIColor(red: 54/255, green: 55/255, blue: 56/255, alpha: 1.0)
    self.clipsToBounds=true

    titleArr = ["Tam Tuyet", "test", "test1"]

    setupViews()

    myTableView.delegate=self
    myTableView.dataSource=self
    myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    myTableView.tableFooterView=UIView()
    myTableView.separatorStyle = UITableViewCellSeparatorStyle.none
    myTableView.allowsSelection = true
    myTableView.bounces=false
    myTableView.showsVerticalScrollIndicator=false
    myTableView.backgroundColor = UIColor.clear
}



}

// testLauncher

class TestLauncher: NSObject{

let frame = UIApplication.shared.keyWindow?.frame

let testContainer: UIView = {
    let view = UIView()
        view.backgroundColor = UIColor.brown
        view.translatesAutoresizingMaskIntoConstraints = false
        view.layer.masksToBounds = true
        view.layer.zPosition = 1

    return view
}()

let btnClose: UIButton = {
    let origImage = UIImage(named: "cancel")
    let tintedImage = origImage?.withRenderingMode(.alwaysTemplate)
    let btnClose = UIButton()
    btnClose.setImage(tintedImage, for: .normal)
    btnClose.tintColor = .white
    btnClose.addTarget(self, action:#selector(handleDismiss), for: UIControlEvents.touchUpInside)
    btnClose.frame = CGRect(x: 10, y: 30, width: 16, height: 16)

    return btnClose
}()

func show() {
    testContainer.addSubview(btnClose)

    if let window = UIApplication.shared.keyWindow {
        window.addSubview(testContainer)

        setUpTestContainer()

        UIView.animate(withDuration: 0.5, delay:0, usingSpringWithDamping: 1, initialSpringVelocity:1, options: .curveEaseOut, animations: {

            self.testContainer.frame = CGRect(x: 0, y: 0, width: (self.frame?.width)!, height: (self.frame?.height)!)

        }, completion: nil)
    }
}


func setUpTestContainer(){
    testContainer.frame = CGRect(x: 0, y: (self.frame?.height)!, width: (self.frame?.width)!, height: (self.frame?.height)!)
}

@objc func handleDismiss() {
    UIView.animate(withDuration: 0.2, animations: {
        if let window = UIApplication.shared.keyWindow{
            self.testContainer.frame = CGRect(x: 0, y: window.frame.height, width:
                self.testContainer.frame.width, height: self.testContainer.frame.height)
        }
    }, completion: {(finished:Bool) in
        self.testContainer.removeFromSuperview()
    })
}

override init() {
    super.init()

}
}

如何删除上一个uiview?我是新手。请帮我。在此先感谢

1 个答案:

答案 0 :(得分:1)

需要在此处进行一些修改

extension TabBarCtr: SidebarViewDelegate {
    func sidebarDidSelectRow(row: Row) {
        blackScreen.isHidden=true
        blackScreen.frame=self.view.bounds
        UIView.animate(withDuration: 0.3) {
            self.sidebarView.frame=CGRect(x: 0, y: 0, width: 0, height: self.sidebarView.frame.height)
        }
        switch row {
        case .Name:
            print(" Name ")
        case .test:
            let test = TestLauncher()
            /// Comment below line
            test.show()

            /// Simply  Present it
            self.present(test, animated: true, completion: nil)
        case .test1:
            print("test1")
        case .none:
            break
        }
    }
}

现在在您的 TestLauncher

更改

class TestLauncher: NSObject

class TestLauncher: UIViewController

添加视图生命周期并保留动画

override func viewDidAppear(_ animated: Bool) {
    self.view.backgroundColor = .white
    self.testContainer.frame = CGRect(x: 0, y: self.view.frame.size.height, width: self.view.frame.size.width, height: self.view.frame.size.height)
    self.view.addSubview(self.testContainer)
    self.testContainer.addSubview(self.btnClose)
    UIView.animate(withDuration: 0.5, delay:0, usingSpringWithDamping: 1, initialSpringVelocity:1, options: .curveEaseOut, animations: {
        self.testContainer.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)

    }, completion: nil)
}

按钮动作

@objc func handleDismiss1(_ sender: UIButton) {
    self.dismiss(animated: true, completion: nil)
}