【问题标题】:Forward event touch with two fingers on UITextView在 UITextView 上用两根手指向前触摸事件
【发布时间】:2012-02-19 10:16:08
【问题描述】:

当用户用两根手指触摸屏幕时,我需要应用一个功能。 我的问题是屏幕有一个UITextView

屏幕上还有活动键盘,这就是通常的方法 (UITapGestureRecognizer) 不适用于此配置的原因。

我该怎么做?

谢谢。

【问题讨论】:

  • 那么您是否想让UITextView 响应触摸?
  • 检查我的答案。它可能会对你有所帮助。

标签: objective-c ios cocoa-touch uitextview multi-touch


【解决方案1】:

您需要将UITapGestureRecognizer 添加到UITextView

这是你必须做的:

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(respondToTap)];

[singleTap setNumberOfTapsRequired:2];
[self.yourTextView addGestureRecognizer:tapRecognizer];


-(void)respondToTap{
//do whatever you have to when textfield is double tapped
}

【讨论】:

  • 感谢您的回答,但它不起作用,我已经尝试过这种方法。问题是我认为 UITextView 不支持 UITapGestureRecognizer。
  • 但这只是一个水龙头,所以它可能无法使用多个。很抱歉造成混乱。
  • 好的,我明白了问题所在,当键盘打开并且光标可用时,使用这种方法我们无法让 UITextView 响应触摸,即使是轻轻一按。在我的情况下,键盘和光标始终可用。所以我需要为这个配置找到解决方案。也许你知道另一种方式?谢谢。
  • 尝试将UITextView 设置为不可编辑。也许这可能会有所帮助,但可能不会。
【解决方案2】:

由于您不能继承 UIKeyboard 以传递触摸,因此此链接可能会对您有所帮助。

Detect if the user has touched the screen

UIWindow 有一个子类 - 如果您将其设置为您的主应用程序窗口(在 appDelegate 中),您可能会在触摸到达键盘之前捕捉到它们。在那里您应该能够检查触摸是否在UITextField 区域中(并正确处理它们)或将它们发送到响应链。

不像使用手势识别器那样优雅,但你可以让它工作。

【讨论】:

    【解决方案3】:

    类似于this answer I gave to a similar question,但需要注意的是,我只在 iOS 10 上使用过此功能,您可以通过使用自定义类扩展 UITextView 并覆盖 addGestureRecognizer: 来做到这一点。

    跟踪 singleTap 只是作为添加两个手指点击的哨兵,因为 UITextView 不断添加和删除手势:

    @interface MyCustomTextView ()
    
    @property (weak, nonatomic) UITapGestureRecognizer *singleTap;
    
    @end
    
    
    @implementation MyCustomTextView
    
    /**
     *  this will fire when the text view is tapped with two fingers
     *
     *  @param tgr
     */
    - (void)_handleTwoTouches:(UITapGestureRecognizer *)tgr
    {
        // ADD CODE HERE
    }
    
    - (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
    {
        [super addGestureRecognizer:gestureRecognizer];
        if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
            UITapGestureRecognizer *tgr = (UITapGestureRecognizer *)gestureRecognizer;
            if ([tgr numberOfTapsRequired] == 1 && [tgr numberOfTouchesRequired] == 1) {
    
                if (!self.singleTap) {
                    self.singleTap = tgr;
    
                    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_handleTwoTouches:)];
                    tap.numberOfTapsRequired = 1;
                    tap.numberOfTouchesRequired = 2;
                    [super addGestureRecognizer:tap];
    
                }
    
            }
        }
    }
    
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 2022-12-28
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多