gpt4 book ai didi

ios - 在 Tableview Cell 上的长按手势上禁用 didSelectRowAtIndexPath

转载 作者:搜寻专家 更新时间:2023-10-31 22:55:32 26 4
gpt4 key购买 nike

我一直在尝试在 UITableView 上设置长按手势识别器。手势工作正常,但我想在识别长按手势时禁用 UITableView 的 didSelectRowAtIndexPath 委托(delegate)功能。

简而言之,如果用户单击单元格,我必须推送一个新的 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
}
} }

最佳答案

@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()
}
}
} }

关于ios - 在 Tableview Cell 上的长按手势上禁用 didSelectRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48787187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com