Swift-addTarget()无法与addSubview()一起使用

时间:2019-04-06 15:57:58

标签: ios swift button

我正在尝试添加从底部显示的横幅:

enter image description here

因此,我创建了一个UIView,其中包含UIButton。问题是,当我单击按钮时,什么也没有发生。

@objc func myButtonAction() {
      print("My Button tapped")
}

func notifBanner(message: String, color: UIColor, backgroundColor: UIColor, button: UIButton){

      let appDelegate = UIApplication.shared.delegate as! AppDelegate
      let window = appDelegate.window!

      if infoViewIsShowing == false{
         infoViewIsShowing = true

         let grey1 = UIColor(red: 196/255, green: 196/255, blue: 196/255, alpha: 1)

         let infoViewHeight = CGFloat(50)
         let infoViewY = window.bounds.height + infoViewHeight

         let infoView = UIView(frame: CGRect(x: 10, y: infoViewY, width: window.bounds.width - 20, height: infoViewHeight))
         infoView.backgroundColor = backgroundColor
         infoView.layer.cornerRadius = 8
         infoView.clipsToBounds = true

         infoView.isUserInteractionEnabled = true
         window.addSubview(infoView)


         infoView.alpha = 0.4

         let widthButton = CGFloat(115)
         let goToProfileButton = UIButton()
         goToProfileButton.frame = CGRect(x: 0, y: 0, width: infoLabelWidth, height: infoLabelHeight)
         goToProfileButton.setTitle("View Profile", for: .normal)
         goToProfileButton.tintColor = .white
         goToProfileButton.addTarget(self, action: #selector(self.myButtonAction), for: .touchUpInside)
         goToProfileButton.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)

         infoView.addSubview(goToProfileButton)

         // Animate bannerView
         UIView.animate(withDuration: 0.4, animations: {

            infoView.alpha = 1
            infoView.frame.origin.y = window.bounds.height - infoViewHeight - 60

         }, completion: { (finished: Bool) in
            if finished{

               UIView.animate(withDuration: 0.4, delay: 3, options: .curveEaseIn, animations: {
                  // move up
                  infoView.frame.origin.y = infoViewY
                  infoView.alpha = 0.3

               }, completion: { (finished: Bool) in
                  if finished {
                     infoView.removeFromSuperview()
                     self.infoViewIsShowing = false
                  }
               })

            }
         })
      }
   }

我尝试添加infoView.isUserInteractionEnabled = true,但仍无法正常工作。您是否知道单击按钮后如何发送操作?

2 个答案:

答案 0 :(得分:1)

假设您希望在希望按钮出现的3秒钟内点击按钮,则可以尝试更改代码以使用DispatchQueue asyncAfter

UIView.animate(withDuration: 0.4, animations: {
    infoView.alpha = 1
    infoView.frame.origin.y = window.bounds.height - infoViewHeight - 60
}, completion: { (finished: Bool) in
    if finished{
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            UIView.animate(withDuration: 0.4, delay: 0, options: .curveEaseIn, animations: {
                // move up
                infoView.frame.origin.y = infoViewY
                infoView.alpha = 0.3
            }, completion: { (finished: Bool) in
                if finished {
                    infoView.removeFromSuperview()
                    self.infoViewIsShowing = false
                }
            })
        }
    }
})

答案 1 :(得分:-1)

您需要添加适用于您的UITapGestureRecognizer

   let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
myView.addGestureRecognizer(tap)

@objc func handleTap(sender: UITapGestureRecognizer? = nil) {
    // handling code
}