等到用户触摸屏幕。迅速

时间:2018-05-24 12:35:35

标签: ios swift wait

发生某些事情后,我会在视图中显示标签

let myView = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
let label = UILabel(frame: CGRect(x:0,y:0,width: 100, height: 100))
myView.addSubview(lebel)
self.view.addSubview(myView)
myView.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
        myView.alpha = 1
    })

现在我要你等到屏幕上点击,然后

UIView.animate(withDuration: 0.5, animations: {
            muView.alpha = 0
        }, completion: { (bool) in
            myView.removeFromSuperview()
    })

我怎么能等到触摸屏幕?

2 个答案:

答案 0 :(得分:1)

只需将tapGesture添加到您的视图

viewDidload

 let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
    self.view.addGestureRecognizer(tapGesture)

handleTapGesture

 @IBAction func handleTapGesture(_ recognizer: UITapGestureRecognizer) {

      UIView.animate(withDuration: 0.5, animations: {
            muView.alpha = 0
           }, completion: { (bool) in
            myView.removeFromSuperview()
       })

    }

答案 1 :(得分:0)

除了Abdelahad的回答之外,您应该将let myView声明为property,以便在@IBAction func handleTapGesture(_ recognizer: UITapGestureRecognizer) {}内使用它。因此,新代码如下:

 @IBAction func handleTapGesture(_ recognizer: UITapGestureRecognizer) {

  UIView.animate(withDuration: 0.5, animations: {
        self.myView.alpha = 0
       }, completion: { (bool) in
        self.myView.removeFromSuperview()
   })
}

最后添加,我发现你的代码中有轻微的错误。 muView错误,myView是正确的。