在 Tableview Cell 上的长按手势上禁用 didSelectRowAtIndexPath
Disable didSelectRowAtIndexPath on Long press gesture on Tableview Cell
我一直在尝试在 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
}
} }
试试这个!
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
}
}
添加这个覆盖方法
override func dismiss(animated flag: Bool,
completion: (() -> Void)?)
{
super.dismiss(animated: flag, completion:completion)
self.tableView.allowsSelection = true
}
@optimus 评论帮助我解决了这个问题,这是一个非常真实的情况,当在 tableview 单元格上识别出长按手势时禁用 didSelectRowAtIndexPath 互联网上没有回答
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()
}
}
} }
我一直在尝试在 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
}
} }
试试这个!
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
}
}
添加这个覆盖方法
override func dismiss(animated flag: Bool,
completion: (() -> Void)?)
{
super.dismiss(animated: flag, completion:completion)
self.tableView.allowsSelection = true
}
@optimus 评论帮助我解决了这个问题,这是一个非常真实的情况,当在 tableview 单元格上识别出长按手势时禁用 didSelectRowAtIndexPath 互联网上没有回答
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()
}
}
} }