ios - 禁用 UITextView 中的点击并按住

标签 ios objective-c uitextfield

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

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

  • caretRectForPosition:,始终返回 CGRectZero 以隐藏插入符

  • addGestureRecognizer:,用于禁用 UITapGestureRecognizerUILongPressGestureRecognizer 手势

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

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

<__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 也不可见(我没有看到带有选择/复制/粘贴等的黑条),只是一个放大镜。

UITextField 所在的 UIViewController 实现:


再说一遍,我确实想要的是:

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

想要的是:

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

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

最佳答案

您应该子类化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

如果您需要任何实现方面的帮助,请告诉我们。

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

相关文章:

ios - 哪个选项卡 Controller 被选中?

ios - inputAccessoryView 无法正常工作或格式化

iphone - 如果更改编辑中的文本,UITextField 会崩溃

ios - 如何将NSString转换为Appengine Blobstore的UIImage

ios - 如何定义将另一个作为条件的预处理器宏?

ios - 切换 uitextf 时停止键盘关闭

ios - 为什么我的 Coregraphics 绘图代码会导致延迟?

ios - 您可以在多少台设备上测试您的 iOS 应用程序?

ios - 如何在执行任何 XCTest 之前运行一次性设置代码

ios - 关于 UIGestureRecognizer