Swift iOS-如何隐藏标签然后让它在一段时间后出现

时间:2017-04-05 12:25:39

标签: ios swift timer viewdidappear hiddenfield

按下按钮时,我有一个隐藏的标签。经过一段时间,如60秒,我希望标签重新出现。我假设我在viewDidAppear中这样做,我该怎么做?

@IBOutlet weak var myLabel: UILabel!

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
       //after 60 secs myLabel should reappear
       //self.myLabel.isHidden = false
    }


@IBAction func buttonTapped(_ sender: UIButton){
       self.myLabel.isHidden = true
}

2 个答案:

答案 0 :(得分:11)

@IBAction func buttonTapped(_ sender: UIButton){
    self.myLabel.isHidden = true
    DispatchQueue.main.asyncAfter(deadline: .now() + 60) {
        self.myLabel.isHidden = false
    }
}

答案 1 :(得分:1)

您可以通过安排计时器来执行此操作:

class ViewController: UIViewController {

    @IBOutlet weak var myLabel: UILabel!

    @IBAction func buttonTapped(sender: UIButton) {
        if !myLabel.isHidden {
            myLabel.isHidden = true
            Timer.scheduledTimer(timeInterval: 15.0, target: self, selector: #selector(showLabel), userInfo: nil, repeats: false)
        }
    }

    func showLabel() {
        myLabel.isHidden = false
    }
}