目前我想让UITextView有一个双击的手势。似乎UITableView有它自己的双击手势,当我们双击时,一些文本将被选中。所以我想在我自己的手势识别器中删除这个默认的双击手势。我尝试了很多方法,都失败了。似乎没有办法删除UITextView的默认识别器。我也想在这个UITextView上添加一个透明的视图来做双击事件,但这个子视图将阻止UITextView上的其他手势。有什么方法可以在UITextView中添加双击手势识别器吗?我真的希望能有一个解决办法。
我仍然期待着iOS5的解决方案:)
发布于 2012-12-10 18:22:48
我在iOS6上有一个解决方案,我们可以使用UIGestureRecognizerDelegate,然后重写gestureRecognizerShouldBegin:和gestureRecognizer:shouldReceiveTouch:。在这两个方法中,我们可以检查手势是否为doubleTapGestureForZooming,如果不是,则返回NO,或者返回YES。这在iOS6中很好用,但是在iOS5中这两个委托方法还没有被调用,所以iOS5可能需要另一个变通方法。最后,我得到了解决办法,我们可以覆盖UITextView的addGestureRecognizer方法来删除默认手势,希望这能对其他人有所帮助。
PS:我们真的不能移除UITextView的系统手势,我们甚至不能改变它们的属性。似乎当事件发生时,UItextview的所有手势都会被再次添加。
发布于 2012-11-29 20:38:09
还有许多其他手势识别器附加到文本视图。因为你似乎并不需要它们。您可以删除它们。
myTextView.gestureRecognizers = nil;在添加双击识别器之前。它起作用了。
然后,您可以添加
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mySelector)];
tapRecognizer.numberOfTapsRequired = 2;
tapRecognizer.numberOfTouchesRequired = 1;
[myTextView addGestureRecognizer:tapRecognizer];发布于 2016-10-02 16:00:56
我知道这个问题有点老了,但为了让未来的搜索者能及时了解这个问题,我想我应该添加另一个从iOS 7到10都适用的解决方案。它基本上汇集了here和here讨论过的解决方案,但对它们进行了微调,使UITextView能够识别自定义双击。
它通过将UITextView子类化并覆盖addGestureRecognizer:方法来实现这一点,以便将我们的自定义回调注入到双击手势中,并将单击手势配置为遵守新的双击钩子。
我之所以在addGestureRecognizer:中这样做,是因为UITextView会根据其当前状态不断地删除和添加手势,因此您必须不断地将其重置回来。
这段代码应该足以让人入门:
@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];
}
@endhttps://stackoverflow.com/questions/13625973
复制相似问题