如何将长按斯威夫特中选择一个表行

问题描述 投票:20回答:8

我有有根据所选内容表中的行运行的代码长按手势识别器的表。

我有麻烦的是,我现在必须点击我要那么做长按排。

我怎样才能让表中选择,我长按,无需挖掘首先选择它的行?

ios swift uigesturerecognizer
8个回答
25
投票

下面的代码工作正常,我:

在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
    }
}

22
投票

斯威夫特4

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 == .ended {
        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
        }
    }
}

Objective - 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
        }
    }
}

19
投票

斯威夫特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)

更多:https://github.com/apple/swift-evolution/blob/e4328889a9643100177aef19f6f428855c5d0cf2/proposals/0046-first-label.md


11
投票

斯威夫特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)")
        }
    }
}

6
投票

对于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)

1
投票

为了避免这种情况,你可以添加qazxsw POI代替qazxsw POI内qazxsw POI


1
投票

使用IBAction为你可以做(​​为的CollectionView):

UILongPressGestureRecognizer

记得用您长按手势识别器链接。


0
投票
cellForRowAtIndexPath
© www.soinside.com 2019 - 2024. All rights reserved.