MasterController: UIViewController
MainViewController: UIViewController
mainCollectionView: UICollectionView
水平具有3个单元格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")
}
}
答案 0 :(得分:0)
我之前曾尝试过shouldRecognizeSimultaneouslyWith otherGestureRecognizer:
,但我想我在某处做错了事。
感谢@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