Ios 如何在swift中隐藏自定义视图中的按钮

Ios 如何在swift中隐藏自定义视图中的按钮,ios,swift,uiview,xib,custom-view,Ios,Swift,Uiview,Xib,Custom View,当点击屏幕时,我想隐藏第三行的按钮。这个屏幕包含三行。我已经添加了触摸事件并尝试了点击手势,但只有当我点击第三行时,如果我点击其他行或点击屏幕上的任何位置,按钮才会隐藏,什么都不会发生。 主要的问题是,当我点击第三行,然后按钮消失,它应该是隐藏的,我们点击屏幕上的任何地方或点击任何单元格。 我正在使用customView在tablewView中显示该视图。TableView是单独的类,自定义视图是单独的类。我正在处理自定义视图类中的按钮 请告诉我解决方案,这是我的代码 class MicTes

当点击屏幕时,我想隐藏第三行的按钮。这个屏幕包含三行。我已经添加了触摸事件并尝试了点击手势,但只有当我点击第三行时,如果我点击其他行或点击屏幕上的任何位置,按钮才会隐藏,什么都不会发生。 主要的问题是,当我点击第三行,然后按钮消失,它应该是隐藏的,我们点击屏幕上的任何地方或点击任何单元格。 我正在使用customView在tablewView中显示该视图。TableView是单独的类,自定义视图是单独的类。我正在处理自定义视图类中的按钮

请告诉我解决方案,这是我的代码

class MicTestView: UIView  {

// MARK:-
// MARK:- Properties

// MARK:- IBOutlets

@IBOutlet weak var aidStatusImageView: UIImageView!
@IBOutlet weak var progressView: UIProgressView!

// MARK:- Class Properties

var micLocation : MicLocation = .bottom
let customInfoView = CustominfoView()

var customView = UIView()

// MARK:-
// MARK:- Methods

// MARK: UIView Methods

required init(coder aDecoder : NSCoder) {
    super.init(coder :aDecoder)!
    setup()
}

override init(frame : CGRect) {
    super.init(frame: frame)
    setup()
}

func setup() {
    let view = loadViewFromXib()
    view.frame = bounds
    // view.autoresizingMask = UIViewAutoresizing.flexibleWidth | UIViewAutoresizing.flexibleHeight

    addSubview(view)

    progressView.progress = 0
    progressView.transform = progressView.transform.scaledBy(x: 1, y: (view.superview?.bounds.height)!/2)
    progressView.isHidden = true
    view.isUserInteractionEnabled = true
}

func loadViewFromXib() -> UIView {
    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: "MicTestView", bundle: bundle)
    let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
    return view
}

// MARK:- IBActions
@IBAction func infoBtnpressed(_ sender: UIButton) {
    customInfoView.isHidden = false

    let customView = customInfoView
    customView.frame = CGRect(x: ((window?.frame.origin.x)! + 60), y: ((window?.frame.origin.y)! + 300), width: 255, height: 200)
    window?.addSubview(customView)
    print("Pressed")

    DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
        self.customInfoView.isHidden = true
    }
}

// MARK: UIViewController overrides
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    self.infoBtn.isHidden = true
    self.customInfoView.isHidden = true
    startRecording()

}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    //
}
类MicTestView:UIView{
//标记:-
//标记:-属性
//标记:-IB出口
@iBMAIDStatusImageView:UIImageView!
@IBVAR progressView:UIProgressView!
//标记:-类属性
变量micLocation:micLocation=.bottom
让customInfoView=customInfoView()
var customView=UIView()
//标记:-
//标记:-方法
//标记:UIView方法
必需的初始化(编码器aDecoder:NSCoder){
super.init(编码器:aDecoder)!
设置()
}
重写初始化(帧:CGRect){
super.init(frame:frame)
设置()
}
函数设置(){
let view=loadViewFromXib()
view.frame=bounds
//view.autoresizingMask=UIViewAutoresizing.flexibleWidth | UIViewAutoresizing.flexibleHeight
添加子视图(视图)
progressView.progress=0
progressView.transform=progressView.transform.scaledBy(x:1,y:(view.superview?.bounds.height)!/2)
progressView.ishiden=true
view.isUserInteractionEnabled=true
}
func loadViewFromXib()->UIView{
let bundle=bundle(for:type(of:self))
让nib=UINib(nibName:“MicTestView”,bundle:bundle)
让view=nib.instantiate(withOwner:self,options:nil)[0]作为!UIView
返回视图
}
//标记:-IBActions
@iAction func infoBtnpressed(u发送方:ui按钮){
customInfoView.ishiden=false
让customView=customInfoView
customView.frame=CGRect(x:((window?.frame.origin.x)!+60),y:((window?.frame.origin.y)!+300),宽:255,高:200)
窗口?.addSubview(自定义视图)
打印(“按下”)
DispatchQueue.main.asyncAfter(截止日期:.now()+5){
self.customInfoView.ishiden=true
}
}
//标记:UIViewController覆盖
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
self.infoBtn.ishiden=true
self.customInfoView.ishiden=true
startRecording()
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
//
}
自定义单元格

protocol tappedDelegate: class {
    func buttonTapped(indexpath: NSIndexPath,btnChange: UIButton)
}

class CustomCell: UITableViewCell {
    var delegate: tappedDelegate?
    var  indexpath1 = NSIndexPath()
    var isOn = Bool()

    @IBOutlet weak var btnClick: UIButton!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    @IBAction func someButtonTapped(sender: UIButton) {
        let btnImag = btnClick.image(for: .normal)

        if btnImag == UIImage (named: "close") {
            btnClick .setImage(UIImage(named: "buttn"), for: .normal)
        }else
        {
            btnClick .setImage(UIImage(named: "close"), for: .normal)

        }

        self.delegate?.buttonTapped(indexpath: indexpath1, btnChange: btnClick)
    }

class ViewController: UIViewController , tappedDelegate


   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "CustomCell"


        var cell: CustomCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? CustomCell

        if cell == nil {
            tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: identifier)
            cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? CustomCell
        }

        cell.delegate = self
        cell.indexpath1 = indexPath as NSIndexPath

        return cell
    }
    func buttonTapped(indexpath: NSIndexPath, btnChange: UIButton) {

   // btnChange .setImage(UIImage(named: "buttn"), for: .normal)

    }

您可以使用protocol delegate在uitableview中隐藏按钮。请给我一个代码示例始终包括解决方案的解释,而不仅仅是代码!