【发布时间】:2018-12-18 14:46:23
【问题描述】:
- 我有一个
MasterController: UIViewController - 里面是一个 UIView(mainViewContainer) -> self.frame
- mainViewContainer 包含
MainViewController: UIViewController - MainViewController 有一个
mainCollectionView: UICollectionView.horizontal 和 3 个单元格 - 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