I am adding a tap gesture recognizer programmatically to a custom collection view cell class. for some reason, it doesnt seem to be working. the frame is not 0, isUserInteractionEnabled is set to true and i made sure the tap view is on top of all other views:
The custom cell class:
let containerView: UIView = {
let view = UIView()
view.isUserInteractionEnabled = true
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let tapView: UIView = {
let v = UIView()
v.isUserInteractionEnabled = true
return v
}()
let tap: UITapGestureRecognizer = {
let t = UITapGestureRecognizer(target: self, action: #selector(tapped))
return t
}()
@objc fileprivate func tapped() {
print("tap")
}
func setTap() {
self.containerView.addSubview(tapView)
tapView.frame = self.frame
// layout constraint code - printing frame shows its not 0 after this
tapView.addGestureRecognizer(tap)
}
In the view controller file that has the collection view:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SuggestCell
cell.category.text = data[indexPath.row].category
cell.word.text = data[indexPath.row].word
cell.setTap()
print(cell.tapView.frame)
return cell
}
I do realize that there is a didSelectItemAt method but I am trying some custom behavior that detects multiple taps to a cell and performs an action