-3

我的应用看起来像这样在此处输入图像描述

然后我点击菜单看起来像这样,然后点击测试 在此处输入图像描述

然后点击按钮关闭在此处输入图像描述

我正在寻找删除最后一个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
}



}

// 测试启动器

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?我是新手。请帮我。感谢先进

4

1 回答 1

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)
}
于 2018-06-25T05:42:11.537 回答