gpt4 book ai didi

ios - 点击手势仅在 3 次点击后响应

转载 作者:行者123 更新时间:2023-11-30 10:57:10 33 4
gpt4 key购买 nike

  1. 我有一个MasterController:UIViewController
  2. 里面是一个 UIView(mainViewContainer) -> self.frame
  3. mainViewContainer包含MainViewController:UIViewController
  4. MainViewController 有一个 mainCollectionView: UICollectionView .horizo​​ntal,有 3 个单元格
  5. mainCollectionView 单元格内部有一个 UITableView

我已将 UITapGestureRecognizer 添加到 MasterController 的 View 中,以在 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")
}


}

最佳答案

我之前曾尝试过 shouldRecognizeSimultaneouslyWith otherGestureRecognizer: 但我想我在某个地方做错了什么。

感谢评论中的@Scriptable,我再次尝试,现在可以工作了。如果没有效果,我们应该总是返回并重试。

通过以下方式解决:

首先向MasterViewController添加委托(delegate)功能->

extension MasterViewController: UIGestureRecognizerDelegate {

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

然后添加委托(delegate)来点击手势 ->

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

tapGesture.delegate = master

关于ios - 点击手势仅在 3 次点击后响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53835540/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com