繁体   English   中英

iOS在Swift中禁用双击手势识别器

[英]iOS Disable Double Tap gesture recognizer in Swift

我现在正在使用TableView开发一个应用程序,但现在遇到以下问题。

  • 在我的TableView里面有UITextView,它必须是可选择的,但不可编辑(因为我需要使用并继续链接)。

我的问题是:
当我像每个人一样点击链接时,它不起作用。 我需要将其保持更长的时间才能使其正常工作。 我认为这是因为“可选”属性带来了双击手势识别器,所以我的textView检查是否还有第二个点击,但是我不知道如何查找和删除仅双击识别器。

我该怎么办?

谢谢。

您是否考虑过用UIWebView替换TextView,仅执行loadHTMLString函数?

这样,当您点击链接时,链接会立即打开吗? 您甚至可以拥有一个UIWebView委托,并在按下链接时执行所需的操作(自定义UIWebView而不是在Safari中自动打开等)

查找和删除双击手势识别器

目标C

- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
  if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) 
  {
     [(UITapGestureRecognizer *)gestureRecognizer setNumberOfTapsRequired:1];
     gestureRecognizer.enabled = NO;
  }
}

迅速

func addGestureRecognizer(gestureRecognizer: UIGestureRecognizer)
{
    if gestureRecognizer.isKindOfClass(UITapGestureRecognizer)
    {
       (gestureRecognizer as! UITapGestureRecognizer).numberOfTapsRequired = 1
       gestureRecognizer.enabled = false
    }

}

您必须处理轻击事件。.通过此代码,tapGesture.numberOfTapsRequired = 1

要么

为此,您需要在UITableViewCell中嵌入一个。 但是无需创建自定义单元。 这是您要做什么的基本思想:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UITextView *comment = [[UITextView alloc] initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, tableView.rowHeight)];
        comment.editable = NO;
        comment.delegate = self;
        [cell.contentView addSubview:comment];
        [comment release];
    }
    return cell;
}

当然,如果您不希望单元格随附的标准44pt高度,则需要设置rowHeight。 而且,如果您需要实际的单元格,则需要添加自己的逻辑,以便仅所需的单元格是textView,但这是基本思想。 其余的则由您定制以适合您的配件。 希望这可以帮助

编辑:绕过textView进入您的单元格,有两种方法可以解决此问题。

1)您可以创建一个自定义textView类并覆盖touchesBe开始将消息发送给super:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
}

这会将触摸事件发送到其父视图,即您的tableView。 考虑到您不想创建自定义UITableViewCells,我想您也可能不想创建自定义textView类。 这导致我选择第二种。

2)创建textView时,删除comment.editable = NO;。 我们需要使其保持可编辑状态,但将使用委托方法对其进行修复。

在您的代码中,您将要插入一个textView委托方法,我们将从那里开始所有工作:

编辑:更改此代码以与UITableViewController一起使用

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
// this method is called every time you touch in the textView, provided it's editable;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:textView.superview.superview];
    // i know that looks a bit obscure, but calling superview the first time finds the contentView of your cell;
    //  calling it the second time returns the cell it's held in, which we can retrieve an index path from;

    // this is the edited part;
    [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    // this programmatically selects the cell you've called behind the textView;


    [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
    // this selects the cell under the textView;
    return NO;  // specifies you don't want to edit the textView;
}

如果那不是您想要的,请让我知道,我们将为您解决

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM