斯威夫特:
像这样定义手势:
fileprivate func addGestures(inCell cell: UITableViewCell, withIndexPath indexPath: IndexPath) {
let tapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(cellTapped))
tapGestureRecognizer.numberOfTapsRequired = 1
tapGestureRecognizer.numberOfTouchesRequired = 1
cell.addGestureRecognizer(tapGestureRecognizer)
let lpgr = UILongPressGestureRecognizer.init(target: self, action: #selector(cellLongPressed))
lpgr.minimumPressDuration = 1.0
cell.addGestureRecognizer(lpgr)
cell.tag = indexPath.row
cell.selectionStyle = .none
}
@objc fileprivate func cellTapped(_ gesture: UITapGestureRecognizer) {
//Do whatever you want to!
}
@objc fileprivate func cellLongPressed(_ gesture: UILongPressGestureRecognizer) {
if gesture.state == .began {
//Do whatever you want to!
}
}
并添加这样的手势:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = (tableView.dequeueReusableCell(withIdentifier: "CellIdentifier") as! UITableViewCell)
self.addGestures(inCell: cell, withIndexPath: indexPath)
}