首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift - addTarget()不使用addSubview()

Swift - addTarget()不使用addSubview()
EN

Stack Overflow用户
提问于 2019-04-06 23:57:58
回答 2查看 147关注 0票数 0

我正在尝试添加一个从底部出现的横幅:

所以我创建了一个内部带有UIButtonUIView。问题是,当我点击按钮时,什么也没有发生。

代码语言:javascript
复制
@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,但它仍然不起作用。你知道当我点击按钮时如何发送动作吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-07 00:47:49

假设您希望能够在按钮出现的3秒内轻击按钮,您可以尝试将代码更改为使用DispatchQueue asyncAfter

代码语言:javascript
复制
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
                }
            })
        }
    }
})
票数 2
EN

Stack Overflow用户

发布于 2019-04-07 00:02:58

您需要添加UITapGestureRecognizer,它将为您工作

代码语言:javascript
复制
   let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
myView.addGestureRecognizer(tap)

@objc func handleTap(sender: UITapGestureRecognizer? = nil) {
    // handling code
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55550849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档