4

目前我想让 UITextView 有一个双击手势。UITableView 似乎有自己的双击手势,当我们双击时,会选择一些文本。所以我想将这个默认的双击手势移除到我自己的手势识别器中。我尝试了很多方法,都失败了。似乎没有办法删除 UITextView 的默认识别器。我还想在这个 UITextView 上添加一个透明视图来执行双击事件,但是这个子视图会阻止 UITextView 上的其他手势。是否有一些方法可以将双击手势识别器添加到 UITextView?我真的希望有一个解决方法。

我仍然期待 iOS5 的解决方法 :)

4

3 回答 3

13

文本视图上附加了许多其他手势识别器。因为你似乎不需要它们。您可以删除它们。

myTextView.gestureRecognizers = nil;

在添加双击识别器之前。有用。

然后你可以添加

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mySelector)];
tapRecognizer.numberOfTapsRequired = 2;
tapRecognizer.numberOfTouchesRequired = 1;
[myTextView addGestureRecognizer:tapRecognizer];
于 2012-11-29T12:38:08.533 回答
2

我在iOS6上有解决方案,我们可以使用UIGestureRecognizerDelegate,并覆盖gestureRecognizerShouldBegin:和gestureRecognizer:shouldReceiveTouch:。在这两个方法中,我们可以检查手势是否为doubleTapGestureForZooming,如果不是返回NO,或者返回YES。这在 iOS6 中完美运行,但在 iOS5 中这两个委托方法尚未被调用,因此 iOS5 可能需要另一种解决方法。最后,我得到了解决方法,我们可以覆盖 UITextView 的 addGestureRecognizer 方法来删​​除默认手势,希望这对其他人有帮助。

PS:UITextView的系统手势我们真的不能移除,我们甚至不能改变他们的属性。似乎当事件发生时,UItextview 的所有手势都会再次添加。

于 2012-12-10T10:22:48.240 回答
1

我知道这个问题很老,但为了让未来的搜索者保持最新,我想我会添加另一个从 iOS 7 到 10 对我有用的解决方案。它基本上汇集了这里这里讨论的解决方案,但对它们进行了调整以获得UITextView识别自定义双击。

它通过子类化UITextView并覆盖该addGestureRecognizer:方法来实现这一点,以便将我们的自定义回调注入双击手势并配置单击手势以尊重新的双击钩子。

我这样做是addGestureRecognizer:因为 aUITextView会根据其当前状态不断删除和添加手势,因此您必须不断将其重置。

这段代码应该足以让某人开始:

@interface MyCustomTextView ()

/**
 *  we want to keep track of the current single-tap gesture so we can make sure
 *  it waits for a double-tap gesture to fail before firing
 */
@property (weak, nonatomic) UITapGestureRecognizer *singleTap;

/**
 *  we want to keep track of the current double-tap gesture so we can tell a single
 *  tap gesture to ignore this double-tap when the single tap gesture changes
 */
@property (weak, nonatomic) UITapGestureRecognizer *doubleTap;

@end


@implementation MyCustomTextView

/**
 *  this will fire when the text view is double-tapped
 *
 *  @param tgr
 */
- (void)_handleTwoTaps:(UITapGestureRecognizer *)tgr
{
    // ADD CODE HERE
}

/**
 *  the reason why I've overridden this methods is these gestures change quite a bit
 *  depending on the state of the UITextView, (eg, when in focus and out of focus)
 *  and so this provides a reliable way to make sure any new gestures get updated
 *  with custom overrides.
 *
 *  @param gestureRecognizer
 */
- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    [super addGestureRecognizer:gestureRecognizer];
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        UITapGestureRecognizer *tgr = (UITapGestureRecognizer *)gestureRecognizer;
        if ([tgr numberOfTapsRequired] == 1 && [tgr numberOfTouchesRequired] == 1) {
            self.singleTap = tgr;
            if (self.doubleTap) {
                [tgr requireGestureRecognizerToFail:self.doubleTap];
            }

        } else if ([tgr numberOfTapsRequired] == 2 && [tgr numberOfTouchesRequired] == 1) {
            [tgr addTarget:self action:@selector(_handleTwoTaps:)];
            self.doubleTap = tgr;
            if (self.singleTap) {
                [self.singleTap requireGestureRecognizerToFail:tgr];
            }
        }
    }
}

// NOTE: I'm not sure if this is needed but it's been there for years
// and so I thought I would include it just in case
- (void)removeGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        UITapGestureRecognizer *tgr = (UITapGestureRecognizer *)gestureRecognizer;
        if ([tgr numberOfTapsRequired] == 2 && [tgr numberOfTouchesRequired] == 1) {
            [tgr removeTarget:self action:@selector(_handleTwoTaps:)];
        }
    }
    [super removeGestureRecognizer:gestureRecognizer];
}

@end
于 2016-10-02T08:00:56.340 回答