我一直在尝试在UITableView上设置长按手势识别器。手势工作正常,但我想在识别长按手势时禁用UITableView的didSelectRowAtIndexPath委托功能。
简而言之,如果用户单击该单元格,我必须推送一个新的UIViewController,如果用户长按该单元格,我必须显示一个UIActionSheet。
extension GroupChatListingViewController : UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
func setupLongPress() {
longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
longPressGesture.cancelsTouchesInView = false
self.tableView.addGestureRecognizer(longPressGesture)
}
func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.began {
self.tableView.allowsSelection = false
let touchPoint = longPressGestureRecognizer.location(in: self.tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
self.showActionSheet()
}
}
else if longPressGestureRecognizer.state == .ended {
self.tableView.allowsSelection = true
}
} }
答案 0 :(得分:1)
override func viewDidLoad() {
super.viewDidLoad()
setupLongPress()
setupTapPress()
}
extension GroupChatListingViewController : UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
func setupLongPress() {
longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
longPressGesture.cancelsTouchesInView = false
self.tableView.addGestureRecognizer(longPressGesture)
}
func setupTapPress() {
singlePressGesture = UITapGestureRecognizer(target: self, action: #selector(tapPress))
singlePressGesture.delegate = self
singlePressGesture.cancelsTouchesInView = false
self.tableView.addGestureRecognizer(singlePressGesture)
}
func tapPress(gesture: UIGestureRecognizer) {
if gesture.state == .ended {
let touchPoint = gesture.location(in: self.tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
// do your task on single tap
}
}
}
func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.began {
let touchPoint = longPressGestureRecognizer.location(in: self.tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
self.showActionSheet()
}
}
} }
答案 1 :(得分:0)
试试这个!
override func dismiss(animated flag: Bool,
completion: (() -> Void)?)
{
super.dismiss(animated: flag, completion:completion)
self.tableView.allowsSelection = true
}
添加此覆盖方法
==