【问题标题】:Disable tap and hold in UITextView在 UITextView 中禁用点击并按住
【发布时间】:2015-05-19 23:54:02
【问题描述】:

有没有办法在 UITextView 中禁用点击并按住

我目前有一个 UITextView 的子类,它实现:

  • caretRectForPosition:,总是返回CGRectZero来隐藏插入符号

  • addGestureRecognizer:,实现禁用UITapGestureRecognizerUILongPressGestureRecognizer手势

在这些方法旁边,我还实现了-canPerformAction:withSender:,它只返回NO,但是这个方法甚至没有被调用(我在那里放了一个断点)。

现在一切正常,我需要它来工作,但遗憾的是用户仍然可以点击并按住文本字段。这会导致出现放大镜,并使文本字段可选。我已经用[self gestureRecognizers] 记录了附加的手势并收到了以下数组:

<__NSArrayI 0x18e50b10>(
< UITapGestureRecognizer: 0x18f60a80; state = Possible; enabled = NO; view = <CDICTextField 0x18f55ff0>; target= <(action=pinFieldTapped:, target=<CDICPINWizardViewController 0x18f53ee0>)>>,
< UITextTapRecognizer: 0x18e344d0; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = <CDICTextField 0x18f55ff0>; target= <(action=oneFingerTripleTap:, target=<UITextInteractionAssistant     0x18e34440>)>; numberOfTapsRequired = 3>,
< UITextTapRecognizer: 0x18e34610; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = <CDICTextField 0x18f55ff0>; target= <(action=oneFingerDoubleTap:, target=<UITextInteractionAssistant     0x18e34440>)>; numberOfTapsRequired = 2>,
< UITextTapRecognizer: 0x18e34740; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = <CDICTextField 0x18f55ff0>; target= <(action=twoFingerSingleTap:, target=<UITextInteractionAssistant     0x18e34440>)>; numberOfTouchesRequired = 2>,
< UITapAndAHalfRecognizer: 0x18e34870; state = Possible; view = <CDICTextField 0x18f55ff0>; target= <(action=tapAndAHalf:, target=<UITextInteractionAssistant 0x18e34440>)>>,
< UILongPressGestureRecognizer: 0x18e33830; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = <CDICTextField 0x18f55ff0>; target= <(action=twoFingerRangedSelectGesture:, target=<    UITextInteractionAssistant 0x18e34440>)>>,
< UITextTapRecognizer: 0x18e34a70; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = <CDICTextField 0x18f55ff0>; target= <(action=oneFingerTap:, target=<UITextInteractionAssistant     0x18e34440>)>>,
< UIVariableDelayLoupeGesture: 0x18e34b90; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = <CDICTextField 0x18f55ff0>; target= <(action=loupeGesture:, target=<UITextInteractionAssistant     0x18e34440>)>>
)

检查数组我确实看到了所有手势报告enabled = NO,除了UITapAndAHalfRecognizer(对于我们开发人员来说,它甚至不是可见的类)。 现在我不确定那个手势是否是罪魁祸首。如果是这样,我也不知道如何禁用它。

我已经搜索过答案,但找不到答案。此外,-canPerformAction:withSender: 没有被调用,这可能是因为没有执行任何操作。 UIMenuViewController 也不可见(我没有看到带有 Select/Copy/Paste 等的黑条),只是一个放大镜。

UITextField 所在的UIViewController 实现:


再一次,我想要的是:

  • 使 UITextfield 可编辑,但不可选择
  • 禁用所有手势(双击、点按并按住、长按)

想要的是:

  • 响应选择/复制/粘贴等的 UITextField。
  • 以任何方式显示放大镜

“为什么”的原因是我正在制作一种 PIN 屏幕,我希望用户在我想要的字段中输入一个数字(因为我必须使用文本字段,请不要提供任何其他创建 PIN 屏幕的方式)。

【问题讨论】:

    标签: ios objective-c uitextfield


    【解决方案1】:

    您应该继承UITextView 并覆盖canPerformAction:withSender。不应提供复制/粘贴的文本字段应使用您的子类定义。

    NonCopyPasteField.h:

    @interface NonCopyPasteField : UITextField
    @end
    

    NonCopyPasteField.m:

    @implemetation
      (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
        if (action == @selector(copy:) || action == @selector(paste:)) {
          return NO;
        }
        [super canPerformAction:action withSender:sender];
      }
    @end
    

    如果您需要有关如何实施它的任何帮助,请告诉我们。

    【讨论】:

    • 谢谢,但正如我在问题中所说的那样;该方法 never 被调用。如果是,我会很高兴(而且可能已经完成了)。
    • 你是否继承了 UITextField?
    • 我已经继承了 UITextField 并且正在使用子类
    • 很奇怪,canPerformAction 没有调用。也许你没有在 xib/storyboard 中设置自定义类?
    • 不,我已经这样做了,愉快地使用了在我的自定义类中声明的函数。但是该方法仍然不会被调用,我什至在 UITextfield 自己的canPerformAction.. 实现中设置了一个断点。
    【解决方案2】:

    我也玩过 pin 输入。 At now 停在下一个子类中:

    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
        return NO;
    }
    
    -(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
        gestureRecognizer.enabled = NO;
    
        if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
            NSInteger taps = ((UITapGestureRecognizer *)gestureRecognizer).numberOfTapsRequired;
            if (taps == 1) {
                gestureRecognizer.enabled = YES;
            }
        }
    
        [super addGestureRecognizer:gestureRecognizer];
    
        return;
    }
    

    但在某些情况下会发生文本选择。

    【讨论】:

    • 那你有没有发现什么?
    • 还没有,还是用这个吧。适合我的目的。
    • Apple 将重新启用一些手势识别器,因此您实际上需要覆盖 gestureRecognizerShouldBegin
    猜你喜欢
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    相关资源
    最近更新 更多