【问题标题】:iOS Disable Double Tap gesture recognizer in SwiftiOS 在 Swift 中禁用双击手势识别器
【发布时间】:2016-12-26 12:26:54
【问题描述】:

我正在使用TableView 开发一个应用程序,现在我遇到了下面列出的问题。

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

我的问题是:
当我像所有人一样点击链接时,它不起作用。我需要再坚持一会儿才能让它发挥作用。我认为这是因为“可选”属性引入了双击手势识别器,所以我的 textView 会检查是否有第二次点击,但我不知道如何查找和删除只有双击识别器。

我该怎么办?

谢谢。

【问题讨论】:

  • 为什么不能使用长按手势识别器?
  • 显示您的编码以提供帮助
  • @user3182143 没有太多的编码。 UITextView 是在storyBoard 中创建的。在 cellForRowAtIndexPath 我使用属性Text.addAttribute(NSLinkAttributeName, value: linkAddress, range: linkWordRange); 添加一些链接然后只需将属性文本分配给 textView。我需要通过点击轻松打开我的链接。
  • 检查我的代码。它工作正常。

标签: ios objective-c iphone uitableview uigesturerecognizer


【解决方案1】:

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

目标 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
    }

}

【讨论】:

    【解决方案2】:

    您必须处理点击事件.. 通过此代码 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 类并覆盖 touchesBegan 来发送消息给 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;
    }
    

    如果这不是您想要的,请告诉我,我们会帮您解决

    【讨论】:

      【解决方案3】:

      您是否考虑过将 TextView 替换为 UIWebView,然后执行 loadHTMLString 函数?

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

      【讨论】:

      • 很好的解决方案,但 Apple 的 UITextView Class Reference 表示他们不建议在这种情况下将 UITextView 替换为 UIWebView
      猜你喜欢
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多