Ios 斯威夫特:长按手势识别器不起作用
我有一个Ios 斯威夫特:长按手势识别器不起作用,ios,swift,uitableview,uilongpressgesturerecogni,Ios,Swift,Uitableview,Uilongpressgesturerecogni,我有一个uitable视图,我想为每一行添加uilongpressurerecognizer。 我尝试在表格单元格上拖动识别器并为其引用操作,但从未调用过 我也试过了 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: ident
uitable视图
,我想为每一行添加uilongpressurerecognizer
。
我尝试在表格单元格上拖动识别器并为其引用操作,但从未调用过
我也试过了
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: ident, for: indexPath) as! TableViewCell
/*...*/
var longGesture = UILongPressGestureRecognizer(target: self, action: #selector(FilterPickerViewController.longPress))
longGesture.minimumPressDuration = 1
cell.leftLabel.addGestureRecognizer(longGesture)
return cell
}
@objc func longPress(_ sender: UILongPressGestureRecognizer) {
print("press")
}
但这也不起作用。我做错了什么?您必须将长按手势识别器添加到表格视图中:
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);
}
}
泰,这很有效。你能告诉我[lpgr release]这一行需要什么,以及它是如何翻译成swift的吗?不过,如果没有它,它似乎可以工作