【问题标题】:Swift - addTarget() not working with addSubview()Swift - addTarget() 不能与 addSubview() 一起使用
【发布时间】:2019-04-06 15:57:57
【问题描述】:

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

所以我创建了一个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 但它仍然无法正常工作。当我点击按钮时,您知道如何发送操作吗?

【问题讨论】:

  • 如果你做了infoView.isUserInteractionEnabled = false呢?
  • @Sweeper 仍然无法正常工作
  • 在 Xcode 中调试视图层次结构,确保按钮在其父视图的框架内,并确保按钮没有被任何东西覆盖。
  • 问题出在 animate() 中,如果没有动画,则无法正常工作。但是不知道怎么保留动画,保留addTarget函数

标签: ios swift button


【解决方案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
                }
            })
        }
    }
})

【讨论】:

  • 不知道为什么简单地将原始代码中的 .curveEaseIn 更改为 [ .curveEaseIn, .allowUserInteraction ] 不起作用。
  • 事实上,通过这个更改,按钮可以工作,但仅在第一个动画期间(当视图从 tabBar 的底部移动到顶部时)
  • 使用我的答案中的代码,按钮应该只在按钮静止的 3 秒内工作。通过将.allowUserInteraction 添加到对UIView.animate 的两个调用中,该按钮也应该在两个短动画中都可以使用。
  • 是的,完美!非常感谢
【解决方案2】:

你需要添加 UITapGestureRecognizer 它会为你工作

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 2020-12-07
    相关资源
    最近更新 更多