UITapGestureRecognizer使用swift4隐藏和取消隐藏标签数据

时间:2018-06-27 07:46:15

标签: ios swift4

我需要UITapGestureRecognizer才能隐藏和取消隐藏该值。单击的用户应隐藏标签值,单击的用户应取消隐藏标签,有什么方法可以执行此操作?

现在,我已使用轻击和长按手势来执行此操作。下面是我的代码,

let tab = UITapGestureRecognizer(target: self, action: #selector(availabelBalance))
tab.numberOfTapsRequired = 1
tab.cancelsTouchesInView = false
accountBalanceView.addGestureRecognizer(tab)

let tabTwo = UILongPressGestureRecognizer(target: self, action: #selector(availabelBalanceTwo))
accountBalanceView.addGestureRecognizer(tabTwo)

3 个答案:

答案 0 :(得分:0)

您只需要tapGesture

let tab = UITapGestureRecognizer(target: self, action: #selector(availabelBalance(_:)))
tab.numberOfTapsRequired = 1
tab.cancelsTouchesInView = false
accountBalanceView.addGestureRecognizer(tab)

//

@objc func availabelBalance(_ sender:UITapGestureRecognizer) {

    if lbl.text == "" {

        lbl.text = value
    }
    else {

         lbl.text = ""
    }
}

答案 1 :(得分:0)

如果您隐藏标签,那么您将无法再次触摸该标签,因为它已被隐藏。

要隐藏标签,您可以使用此技巧。

当您点击标签时,您可以检查...

var tempText = "" //temperory property to store value or label

@objc func tapDetected(_ sender: UITapGestureRecognizer) {

    if let text = label.text, !text.isEmpty {
        tempText = lbl.text.text
        label.text = " "
    } else {
        label.text = tempText 
    }
}

答案 2 :(得分:0)

这是示例代码

import UIKit

class firstViewController: UIViewController {

@IBOutlet var textLbl: UILabel!
var tab : UITapGestureRecognizer?
override func viewDidLoad() {
    super.viewDidLoad()

    tab = UITapGestureRecognizer(target: self, action: #selector(availabelBalance))
    tab?.numberOfTapsRequired = 1
    tab?.cancelsTouchesInView = false
    self.view.addGestureRecognizer(tab!)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@objc func availabelBalance(_ sender:UITapGestureRecognizer) {

    if (tab?.cancelsTouchesInView)! {
        textLbl.isHidden = true
        tab?.cancelsTouchesInView = false
    }else{
        textLbl.isHidden = false
        tab?.cancelsTouchesInView = true
    }

}

}