首页 > 解决方案 > 等到用户触摸屏幕。迅速

问题描述

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

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()
    })

我怎样才能等到我触摸屏幕?

标签: iosswiftwait

解决方案


只需将 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()
       })

    }

推荐阅读