【问题标题】:uitapgesture responds only after 3 taps轻按手势仅在轻按 3 次后响应
【发布时间】:2018-12-18 14:46:23
【问题描述】:
  1. 我有一个MasterController: UIViewController
  2. 里面是一个 UIView(mainViewContainer) -> self.frame
  3. mainViewContainer 包含MainViewController: UIViewController
  4. MainViewController 有一个 mainCollectionView: UICollectionView .horizo​​ntal 和 3 个单元格
  5. mainCollectionView 单元格内部有一个UITableView

我在 MasterController 的视图中添加了一个 UITapGestureRecognizer 以在 UITableView 中执行操作。

它在第一个 CollectionViewCell 中运行良好,但是当我滚动到下一个单元格时,点击手势仅在点击 3 次后才会响应,但我希望它在第一次点击时响应,就像在第一个单元格中一样。

注意:在 3 次点击后,它开始在单击时正常工作。只有在构建后初始化应用时才会发生这种情况。

这是在我的 tableViewCell 中:

class CustomTableViewCell: UITableViewCell {

  let customView: UIView = {
     let view = UIView()
     view.backgroundColor = .white
     view.translatesAutoresizingMaskIntoConstraints = false
     return view
  }()

  let moreButton: UIButton = {
        let button = UIButton(type: .system)
        button.setImage(#imageLiteral(resourceName: "moreButton").withRenderingMode(.alwaysOriginal), for: .normal)
        button.contentMode = .scaleAspectFit
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

  lazy var tapGesture: UITapGestureRecognizer = {
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
        tapGesture.cancelsTouchesInView = false
        return tapGesture
    }()

  var master = UIApplication.shared.keyWindow?.rootViewController as? MasterViewController

  var isCustomViewOpen = false

  override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        setupCustomView()
        setupMoreButton()

        master?.view.addGestureRecognizer(tapGesture)
  }

  func setupCustomView() {
        customView.layer.cornerRadius = 7
        addSubview(customView)
        NSLayoutConstraint.activate([
            customView.topAnchor.constraint(equalTo: topAnchor, constant: 10),
            customView.leftAnchor.constraint(equalTo: leftAnchor, constant: 10),
            customView.rightAnchor.constraint(equalTo: rightAnchor, constant: -10),
            customView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -10),
            ])
    }

    func setupMoreButton() {
        customView.addSubview(moreButton)
        moreButton.addTarget(self, action: #selector(handleMoreButton(_:)), for: .touchDown)
        NSLayoutConstraint.activate([
            moreButton.heightAnchor.constraint(equalToConstant: 15),
            moreButton.widthAnchor.constraint(equalToConstant: 20),
            moreButton.centerYAnchor.constraint(equalTo: customView.centerYAnchor),
            moreButton.trailingAnchor.constraint(equalTo: customView.trailingAnchor, constant: -20)
            ])
    }

    @objc func handleMoreButton(_ sender: UIButton) {
        isCustomViewOpen ? slideCustomViewRight() : slideCustomViewLeft()
    }

    func performAnimations(transform: CGAffineTransform) {
        UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            self.customView.transform = transform
        })
    }

    func slideCustomViewLeft() {
        isCustomViewOpen = true
        performAnimations(transform: CGAffineTransform(translationX: -self.frame.width/3.2, y: 0))
        tapGesture.isEnabled = true
    }

    func slideCustomViewRight() {
        isCustomViewOpen = false
        performAnimations(transform: .identity)
        tapGesture.isEnabled = false
    }

    @objc func handleTapGesture(_ sender: UITapGestureRecognizer) {
        slideCustomViewRight()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


}

【问题讨论】:

  • 点击手势与集合视图自己的手势冲突。您可以将它们配置为一起工作,但这可能会导致其他问题。你的点击手势到底是做什么的?或者它需要什么?
  • @Scriptable 表格视图行是显示任务的自定义 uiview。我添加了一个按钮来将此自定义视图向左滑动并显示另一个带有垃圾按钮的视图。所以在其他任何地方点击手势都会关闭这个自定义视图。
  • 尝试 func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)touchesEnded 并检查点击位置,如果在您的视图之外 - 关闭自定义视图

标签: ios swift uitableview uicollectionview uitapgesturerecognizer


【解决方案1】:

我之前尝试过shouldRecognizeSimultaneouslyWith otherGestureRecognizer:,但我想我在某处做错了什么。

感谢 cmets 中的@Scriptable,我再次尝试并现在可以使用。如果没有任何效果,我们应该总是回去重试。

通过以下方式解决:

首先向 MasterViewController 添加委托函数 ->

extension MasterViewController: UIGestureRecognizerDelegate {

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

然后将委托添加到点击手势 ->

var master = UIApplication.shared.keyWindow?.rootViewController as? MasterViewController

tapGesture.delegate = master

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 2020-12-09
    • 1970-01-01
    相关资源
    最近更新 更多