194

UITableViewCell「クイックアクセスメニュー」を印刷するために長押しを処理したいと思います。誰かがすでにこれをしましたか?

特にジェスチャはUITableView

4

10 に答える 10

434

まず、長押しジェスチャ認識機能をテーブルビューに追加します。

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
  initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.myTableView addGestureRecognizer:lpgr];
[lpgr release];

次に、ジェスチャハンドラで:

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    CGPoint p = [gestureRecognizer locationInView:self.myTableView];

    NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];
    if (indexPath == nil) {
        NSLog(@"long press on table view but not on a row");
    } else if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        NSLog(@"long press on table view at row %ld", indexPath.row);
    } else {
        NSLog(@"gestureRecognizer.state = %ld", gestureRecognizer.state);
    }
}

これは、ユーザーによる通常のセルのタップを妨げないように注意する必要があります。また、handleLongPress複数回起動する可能性があることに注意してください(これはジェスチャレコグナイザーの状態が変化するためです)。

于 2010-10-13T14:49:59.387 に答える
46

私はAnna-Kareninaの回答を使用しましたが、非常に深刻なバグでほとんどうまく機能します.

セクションを使用している場合、セクション タイトルを長押しすると、そのセクションの最初の行を押すという間違った結果が得られます。以下に修正バージョンを追加しました (ジェスチャの状態に基づくダミー コールのフィルタリングを含む)。アンナ・カレーニナの提案)。

- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {

        CGPoint p = [gestureRecognizer locationInView:self.tableView];

        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
        if (indexPath == nil) {
            NSLog(@"long press on table view but not on a row");
        } else {
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
            if (cell.isHighlighted) {
                NSLog(@"long press on table view at section %d row %d", indexPath.section, indexPath.row);
            }
        }
    }
}
于 2013-01-16T17:21:40.653 に答える
21

これは、Dawn Song の回答と Marmor の回答を組み合わせた明確な説明です。

長押し Gesture Recognizer をドラッグして、テーブル セルにドロップします。左側のリストの一番下にジャンプします。

ここに画像の説明を入力

次に、ボタンを接続するのと同じ方法でジェスチャ レコグナイザーを接続します。 ここに画像の説明を入力

アクション ハンドラに Marmor のコードを追加します。

- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {

    CGPoint p = [sender locationInView:self.tableView];

    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
    if (indexPath == nil) {
        NSLog(@"long press on table view but not on a row");
    } else {
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        if (cell.isHighlighted) {
            NSLog(@"long press on table view at section %d row %d", indexPath.section, indexPath.row);
        }
    }
}

}

于 2015-08-22T08:37:05.350 に答える
14

次に示すように、認識機能をセルに直接追加する方が効率的です。

TableView セルのタップ アンド ホールド、その後と現在

(一番下の例までスクロールします)

于 2011-08-21T12:44:30.103 に答える
12

スウィフトで答えてください:

UIGestureRecognizerDelegateUITableViewControllerにデリゲートを追加します。

UITableViewController 内:

override func viewDidLoad() {
    super.viewDidLoad()

    let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")
    longPressGesture.minimumPressDuration = 1.0 // 1 second press
    longPressGesture.delegate = self
    self.tableView.addGestureRecognizer(longPressGesture)

}

そして機能:

func handleLongPress(longPressGesture:UILongPressGestureRecognizer) {

    let p = longPressGesture.locationInView(self.tableView)
    let indexPath = self.tableView.indexPathForRowAtPoint(p)

    if indexPath == nil {
        print("Long press on table view, not row.")
    }
    else if (longPressGesture.state == UIGestureRecognizerState.Began) {
        print("Long press on row, at \(indexPath!.row)")
    }

}
于 2016-02-26T16:11:46.780 に答える
5

最新の構文を使用し、他の回答を組み込み、不要なコードを排除するSwift 3の回答。

override func viewDidLoad() {
    super.viewDidLoad()
    let recognizer = UILongPressGestureRecognizer(target: self, action: #selector(tablePressed))
    tableView.addGestureRecognizer(recognizer)
 }

@IBAction func tablePressed(_ recognizer: UILongPressGestureRecognizer) {
    let point = recognizer.location(in: tableView)

    guard recognizer.state == .began,
          let indexPath = tableView.indexPathForRow(at: point),
          let cell = tableView.cellForRow(at: indexPath),
          cell.isHighlighted
    else {
        return
    }

    // TODO
}
于 2017-10-04T23:13:21.227 に答える
2

UILongPressGestureRecognizer をストーリーボードの特定のプロトタイプ セルに追加し、ジェスチャーを viewController の .m ファイルにプルしてアクション メソッドを作成するだけです。言われた通りにしました。

于 2015-01-30T06:19:29.633 に答える
-2

touchesBeganのUITouchタイムスタンププロパティを使用して、touchesEndedが起動されたときにタイマーを起動または停止します

于 2010-10-13T13:58:30.760 に答える