【发布时间】:2015-06-15 07:13:04
【问题描述】:
我有一个表格,它有一个长按手势识别器,它根据选择的表格行运行代码。
我遇到的麻烦是我目前必须点击我想要的行然后长按。
如何让表格选择我长按的行,而无需先点击选择它?
【问题讨论】:
标签: ios swift uigesturerecognizer
我有一个表格,它有一个长按手势识别器,它根据选择的表格行运行代码。
我遇到的麻烦是我目前必须点击我想要的行然后长按。
如何让表格选择我长按的行,而无需先点击选择它?
【问题讨论】:
标签: ios swift uigesturerecognizer
Swift 4 和 5
override func viewDidLoad() {
super.viewDidLoad()
setupLongPressGesture()
}
func setupLongPressGesture() {
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
self.tblMessage.addGestureRecognizer(longPressGesture)
}
@objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer){
if gestureRecognizer.state == .began {
let touchPoint = gestureRecognizer.location(in: self.tblMessage)
if let indexPath = tblMessage.indexPathForRow(at: touchPoint) {
}
}
}
斯威夫特 3
override func viewDidLoad() {
super.viewDidLoad()
setupLongPressGesture()
}
func setupLongPressGesture() {
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(YourViewController.handleLongPress(_:)))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
self.tblMessage.addGestureRecognizer(longPressGesture)
}
func handleLongPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.Began {
let touchPoint = longPressGestureRecognizer.locationInView(self.view)
if let indexPath = tableView.indexPathForRowAtPoint(touchPoint) {
// your code here, get the row for the indexPath or do whatever you want
}
}
}
目标 - C
UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPress:)];
[self.tableView addGestureRecognizer:longPressRecognizer];
-(void)onLongPress:(UILongPressGestureRecognizer*)pGesture
{
if (pGesture.state == UIGestureRecognizerStateRecognized)
{
//Do something to tell the user!
}
if (pGesture.state == UIGestureRecognizerStateEnded)
{
UITableView* tableView = (UITableView*)self.view;
CGPoint touchPoint = [pGesture locationInView:self.view];
NSIndexPath* row = [tableView indexPathForRowAtPoint:touchPoint];
if (row != nil) {
//Handle the long press on row
}
}
}
【讨论】:
indexPathForRowAtPoint 值为零
以下代码对我来说很好用:
在 viewDidLoad 中添加长按手势识别器:
// tapRecognizer, placed in viewDidLoad
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPress:")
self.view.addGestureRecognizer(longPressRecognizer)
那么长按调用的方法是这样的:
//Called, when long press occurred
func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.Began {
let touchPoint = longPressGestureRecognizer.locationInView(self.view)
if let indexPath = tableView.indexPathForRowAtPoint(touchPoint) {
// your code here, get the row for the indexPath or do whatever you want
}
}
【讨论】:
let touchPoint = longPressGestureRecognizer.locationInView(self.tableView) 是正确的,而不是 let touchPoint = longPressGestureRecognizer.locationInView(self.view)。否则,您不会考虑到self.tableView.frame.origin 不能等于CGPointZero
Swift 3 功能:
func handleLongPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.Began {
let touchPoint = longPressGestureRecognizer.locationInView(self.view)
if let indexPath = tableView.indexPathForRowAtPoint(touchPoint) {
// your code here, get the row for the indexPath or do whatever you want
}
}
viewDidLoad:
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(YourViewController.handleLongPress(_:)))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
self.tableView.addGestureRecognizer(longPressGesture)
【讨论】:
Swift 4
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed(sender:)))
self.view.addGestureRecognizer(longPressRecognizer)
// MARK:动作
@objc func longPressed(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizerState.began {
let touchPoint = sender.location(in: self.tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
print("Long pressed row: \(indexPath.row)")
}
}
}
【讨论】:
适用于 swift 版本 3
func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.began {
let touchPoint = longPressGestureRecognizer.location(in: self.view)
if let indexPath = notificationTabelView.indexPathForRow(at: touchPoint) {
print("indexPath=\(indexPath)")
// your code here, get the row for the indexPath or do whatever you want
}
}
}
在您的viewDidLoad 函数中
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(EmployeeNotificationViewController.longPress(_:)))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self as? UIGestureRecognizerDelegate
self.notificationTabelView.addGestureRecognizer(longPressGesture)
【讨论】:
为避免这种情况,您可以在cellForRowAtIndexPath 中添加UILongPressGestureRecognizer 而不是didSelectRowAtIndexPath
【讨论】:
使用 IBAction 你可以做(对于 CollectionView):
@IBAction func handleLongPress(sender: AnyObject) {
if sender.state == UIGestureRecognizerState.Began
{
let position = sender.locationInView(sender.view)
if let indexPath : NSIndexPath = ((sender.view as! UICollectionView).indexPathForItemAtPoint(position))!{
print("You holding cell #\(indexPath.item)!")
}
}
}
记得链接你的长按手势识别器。
【讨论】:
let longPressGesture = UILongPressGestureRecognizer(target: self, action: (#selector(YourCustomeTableCell.longTap)))
self.addGestureRecognizer(longPressGesture)
func longTap(){
print("Long tap")
}
【讨论】:
斯威夫特 5
像这样在viewDidLoad() 中声明这一行
override func viewDidLoad() {
super.viewDidLoad()
//do other stuff here
// long press listener for tableview
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(sender:)))
tableView.addGestureRecognizer(longPress)
}
handleLongPress() 方法在哪里
@objc private func handleLongPress(sender: UILongPressGestureRecognizer) {
if sender.state == .began {
let touchPoint = sender.location(in: tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
let alert = UIAlertController(title: "Alert", message: "Do you want to delete this item?", preferredStyle: .alert)
let action = UIAlertAction(title: "Yes", style: .default) { (action) in
// do your functionality
alert.dismiss(animated: true, completion: nil)
}
let actionDelete = UIAlertAction(title: "No", style: .default) { (action) in
// do your functionality
alert.dismiss(animated: true, completion: nil)
}
alert.addAction(action)
alert.addAction(actionDelete)
self.present(alert, animated: true, completion: nil)
// your code here, get the row for the indexPath or do whatever you want
}
}
}
【讨论】: