简体   繁体   English

如何在UITextField和UISearchBar中禁用长按手势识别器?

[英]How to disable Long press gesture recogniser in UITextField and UISearchBar?

I come across many post about disabling long press in UItextView, but that same process is not working for UITextField and UISearchBar. 我遇到很多关于在UItextView中禁用长按的帖子,但是同样的过程对UITextField和UISearchBar不起作用。

Is there any way to disable Long press on UITextField and UISearchBar so I can avoid Magnifying glass on long press? 有没有办法禁用长按UITextField和UISearchBar所以我可以避免长按放大镜?

I have already checked " Disable Magnifying Glass in UITextField " but solution given there is not actually disabling magnifying glass. 我已经检查过“ 在UITextField中禁用放大镜 ”但是解决方案实际上并没有禁用放大镜。 Its just not allowing cursory to Move between text. 它只是不允许粗略地在文本之间移动。 but still displaying Magnifying glass. 但仍然显示放大镜。

I want to disable Long Press - to avoid Problem which I am facing with magnifying glass. 我想禁用长按 - 以避免我用放大镜面对的问题 I want to disable it now. 我想现在禁用它。 will enable it again when I will able to fix this. 当我能够解决这个问题时,我会再次启用它。

You can try following for disabling only built in long press gesture 您可以尝试以下禁用仅内置长按手势

for (UIGestureRecognizer *recognizer in textView.gestureRecognizers) {
  if ([recognizer isKindOfClass:[UILongPressGestureRecognizer class]]){
    recognizer.enabled = NO;
  }
}

or 要么

delegate method for all :- 所有代表方法: -

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return NO;
}

@Neeraj answer in Swift 4 : @Neeraj在Swift 4中回答:

class CustomTextView: UITextView {

   override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {

     if gestureRecognizer.isKind(of: UILongPressGestureRecognizer.self) {
              gestureRecognizer.isEnabled = false
     }
    return super.addGestureRecognizer(gestureRecognizer)
   }
}

This combination in a custom class is the only thing I've been able to get to work for me, and on the first long press it still sometimes makes the magnifying glass appear: 这个组合在一个自定义类中是我唯一能够为我工作的东西,并且在第一次长按时它仍然有时会使放大镜出现:

//swift 4.2
override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer)
{

    if gestureRecognizer.isKind(of: UILongPressGestureRecognizer.self)
    {
        print(gestureRecognizer.name.unwrappedDebugString)
        gestureRecognizer.isEnabled = false
    }
    return super.addGestureRecognizer(gestureRecognizer)
}
override func becomeFirstResponder() -> Bool
{
    for recognizer in self.gestureRecognizers ?? []
    {
        if (recognizer is UILongPressGestureRecognizer)
        {
            recognizer.isEnabled = false
        }
    }
    return super.becomeFirstResponder()
}

One good and clean way to do this is you can create a custom class for the UITextField and in the custom class you can override the long-press gesture which is causing the magnification view appear. 一个好的和干净的方法是你可以为UITextField创建一个自定义类,在自定义类中你可以覆盖导致放大视图出现的长按手势。 This custom class can be used in all places where we don't want to show the magnification view for test field. 此自定义类可用于我们不想显示测试字段的放大视图的所有位置。 Just use below class in place of UITextField class 只需使用下面的类代替UITextField类

class CustomTextView: UITextView {

    override func addGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
        if (gestureRecognizer.isKindOfClass(UILongPressGestureRecognizer)) {
            gestureRecognizer.enabled = false;
        }
        super.addGestureRecognizer(gestureRecognizer)
        return
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM