繁体   English   中英

Swift - addTarget()不能与addSubview()一起使用

[英]Swift - addTarget() not working with addSubview()

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

在此输入图像描述

所以我在里面用UIButton创建了一个UIView 问题是当我点击按钮时,没有任何反应。

@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但它仍然无法正常工作。 当我点击按钮时你对我如何发送动作有任何想法吗?

假设您希望能够在按钮出现的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
                }
            })
        }
    }
})

你需要添加UITapGestureRecognizer它会对你有用

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM