UITableViewCell
「クイックアクセスメニュー」を印刷するために長押しを処理したいと思います。誰かがすでにこれをしましたか?
特にジェスチャはUITableView
?
UITableViewCell
「クイックアクセスメニュー」を印刷するために長押しを処理したいと思います。誰かがすでにこれをしましたか?
特にジェスチャはUITableView
?
まず、長押しジェスチャ認識機能をテーブルビューに追加します。
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
複数回起動する可能性があることに注意してください(これはジェスチャレコグナイザーの状態が変化するためです)。
私は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);
}
}
}
}
これは、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);
}
}
}
}
スウィフトで答えてください:
UIGestureRecognizerDelegate
UITableViewControllerにデリゲートを追加します。
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)")
}
}
最新の構文を使用し、他の回答を組み込み、不要なコードを排除する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
}
UILongPressGestureRecognizer をストーリーボードの特定のプロトタイプ セルに追加し、ジェスチャーを viewController の .m ファイルにプルしてアクション メソッドを作成するだけです。言われた通りにしました。
touchesBeganのUITouchタイムスタンププロパティを使用して、touchesEndedが起動されたときにタイマーを起動または停止します