12

UITextview长按触摸时,放大镜会出现。我怎样才能禁用它。

4

5 回答 5

34

最后这个问题也解决了

这是供任何人需要的参考代码

在子类 UITextview 的 m 文件中添加了代码

-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
{
    //Prevent zooming but not panning
    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) 
    {
        gestureRecognizer.enabled = NO;
    }
    [super addGestureRecognizer:gestureRecognizer];
    return;
}

有用。

于 2012-05-17T18:11:35.737 回答
1

这对我有用

@implementation CustomTextView 

- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
    self.selectedTextRange = nil;
    return nil;
}

- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    gestureRecognizer.delegate = self;

    [super addGestureRecognizer:gestureRecognizer];
    return;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    return NO;
}

- (CGRect)caretRectForPosition:(UITextPosition *)position
{
    return [super caretRectForPosition:self.endOfDocument];
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] && !gestureRecognizer.delaysTouchesEnded))
    {
        return NO;
    }
    else
        return YES;

}
于 2016-06-03T09:27:24.277 回答
1

Swift 4 版本的@user1120133 的回答:

    override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {
      //Prevent long press to show the magnifying glass
      if gestureRecognizer is UILongPressGestureRecognizer {
        gestureRecognizer.isEnabled = false
      }

      super.addGestureRecognizer(gestureRecognizer)
    }
于 2019-01-09T10:42:44.190 回答
1

Xamarin.iOS:

  1. 创建自定义 UITextView
  2. 覆盖GestureRecognizerShouldBegin您的方法UITextView
public override bool GestureRecognizerShouldBegin(UIGestureRecognizer gestureRecognizer)
{
    if (gestureRecognizer is UILongPressGestureRecognizer ||
        gestureRecognizer.Name != "UITextInteractionNameLinkTap")
    {
        return false;
    }
    return true;
}
于 2019-04-30T11:51:15.743 回答
-1

@Irina 的答案部分适用于 iOS 9.x(尝试轻按,然后长按,您将获得放大覆盖),并在 iOS 10 上崩溃,原因如下:

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“您无法更改 UIViewControllerPreviewing 失败关系手势识别器的委托”

以下代码适用于 iOS 9.x 和 10.x,适用于我能想到的每种点击和/或长手势组合。

请注意,我不保证它会被 Apple 的审核接受

@implementation CustomTextView

- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    NSArray *allowedGestures = @[ @"UILongPressGestureRecognizer", @"UIScrollViewDelayedTouchesBeganGestureRecognizer", @"UIScrollViewPanGestureRecognizer" ];

    if (![allowedGestures containsObject:NSStringFromClass([gestureRecognizer class])])
    {
        return;
    }

    if (([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] && !gestureRecognizer.delaysTouchesEnded))
    {
        return;
    }

    [super addGestureRecognizer:gestureRecognizer];
}

@end

我们需要UIScrollViewDelayedTouchesBeganGestureRecognizer并且UIScrollViewPanGestureRecognizer为了保持UITextView滚动的能力。这两个类都是私有 API 的一部分,因此使用它需要您自担风险。

于 2016-10-25T14:39:18.670 回答