According to this thread, adding a Gesture recognizer can conflict with the TableView interactions.
Hence, To achieve what you require, you will have to add a gesture recognizer to the contentView
of the UITableViewCell
and get the tapped location of the gesture. For this,
- First, Define the
UITapGestureRecognizer
and the action within the UITableViewCell
class. Refer the following code
lazy var tap = UITapGestureRecognizer(target: self, action: #selector(didTapScreen))
.
.
.
// Gesture action
@objc func didTapScreen(touch: UITapGestureRecognizer) {
let xLoc = touch.location(in: self.contentView).x // Getting the location of tap
if xLoc > contentView.bounds.width/2 {
// RIGHT
} else {
// LEFT
}
}
- Add the following to the
init()
method of the custom cell as follows
override init(frame: CGRect) {
super.init(frame: frame)
tap.numberOfTapsRequired = 1
contentView.addGestureRecognizer(tap)
// Other setups
}
I tried the code in a UICollectionViewCell
and got the following output
