ios - 将 GestureRecognizer 添加到 UIView 不会触发操作

标签 ios swift uitapgesturerecognizer

我不知道为什么这个手势识别器没有按预期工作:

class SlideInMenuLauncher: NSObject, UITableViewDelegate, UITableViewDataSource {

  fileprivate let dimmerView = UIView()
  fileprivate let tableView: UITableView = {
    let tbv = UITableView(frame: .zero)
    return tbv
  }()
  fileprivate let resourceArray: [Content]
  weak var delegate: SlideInMenuDelegate?

  let cellIdentifier = "SlideInMenuTableViewCell"

  init(withContentArray contentArray: [Content]) {

    resourceArray = contentArray

    super.init()

    tableView.dataSource = self
    tableView.delegate = self

    tableView.register(UINib(nibName: cellIdentifier, bundle: nil), forCellReuseIdentifier: cellIdentifier)
  }

  func showMenu() {
    if let window = UIApplication.shared.keyWindow {
      dimmerView.backgroundColor = UIColor(white: 0, alpha: 0.5)

      dimmerView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissMenu)))
      dimmerView.isUserInteractionEnabled = true

      window.addSubview(dimmerView)
      window.addSubview(tableView)

      let height: CGFloat = 200
      let y = window.frame.height - height
      tableView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: height)

      dimmerView.frame = window.frame
      dimmerView.alpha = 0

      UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
        self.dimmerView.alpha = 1
        self.tableView.frame = CGRect(x: 0, y: y, width: self.tableView.frame.width, height: self.tableView.frame.height)
      }, completion: nil)

    }
  }

  func dismissMenu() {
    UIView.animate(withDuration: 0.5) {
      self.dimmerView.alpha = 0

      if let window = UIApplication.shared.keyWindow {
        self.tableView.frame = CGRect(x: 0, y: window.frame.height, width: self.tableView.frame.width, height: self.tableView.frame.height)
      }
    }
  }

我在 dismissMenu 函数中放置了一个断点,但它从未被触发。也许我疲惫的眼睛(和头脑)错过了一些简单的东西?

在这里我添加了我实际调用这个类的代码,以防出现问题:

///Some other VC
let slideInMenuLauncher = SlideInMenuLauncher(withContentArray: [content])
slideInMenuLauncher.showMenu()

最佳答案

我希望这会产生编译器错误。您在 dismissMenu() 函数中缺少 @objc 声明。

我在 Storyboard 中向 View Controller 添加了一个 View ,并使用了您的代码的一个小变体,它可以很好地响应点击。因此,我的猜测是您将 View 添加到 View Controller 的方式有问题。

编辑:

我知道问题出在哪里:如果将 View 的 alpha 设置为 0,它会停止响应点击。尝试将 View 的不透明标志设置为 false,并将其背景颜色设置为 clearColor。

这是我创建的测试项目的代码:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var tapView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tapView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
        tapView.isUserInteractionEnabled = true
    }

    @objc func handleTap() {
        print("You tapped on the view")
    }
}

关于ios - 将 GestureRecognizer 添加到 UIView 不会触发操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51887006/

相关文章:

ios - 多个UITableView可以使用同一个UITableviewcell吗?

ios - 如何将addtarget设置为自定义UIView

ios - Swift 3、iOS 10.3 - 在启动屏幕期间预加载 UIWebView

iphone - View 中的许多点击手势

ios - 多个 UILabels 点击相同的 UITapGestureRecogniser 不工作

ios - 添加手势识别器以指定表格中单元格的对象

ios - 使用 playlist.m3u8 从 Wowza 流式传输到 iOS 应用程序

ios swift 3 xcode8 beta 圆形 imageView

ios - 意外的 SafariViewController react

iphone - 动态更改应用购买中产品的价格