OGeek|极客世界-中国程序员成长平台

标题: ios - 在 UITextView 中禁用点击并按住 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:09
标题: ios - 在 UITextView 中禁用点击并按住

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

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

除了这些方法之外,我还实现了 -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实现:


再一次,我想要的是:

想要的是:

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



Best Answer-推荐答案


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

NonCopyPasteField.h:

@interface NonCopyPasteField : UITextField
@end

NonCopyPasteField.m:

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

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

关于ios - 在 UITextView 中禁用点击并按住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29985090/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4