滑动手势多次调用该方法

时间:2014-04-17 07:00:09

标签: ios objective-c uitableview uigesturerecognizer

我遇到一个小问题...我有一个自定义UITableViewCell,在其textView我添加了 2个手势UITapGestureUISwipeGesture .. 点击手势工作正常,但轻扫手势 多次调用该方法 ..有时候调用它两次甚至更多次......这是我如何将它们添加到细胞中

//added in cellForRowAtIndexPath Method
UITapGestureRecognizer *tapToTranslate = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToTranslate:)];
[tapToTranslate setNumberOfTapsRequired:1];
[tapToTranslate setNumberOfTouchesRequired:1];
tapToTranslate.delegate = self;
[cell.messageContentView addGestureRecognizer:tapToTranslate];


UISwipeGestureRecognizer *swipeToTranslate = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(translateTo:)];
swipeToTranslate.numberOfTouchesRequired = 1;
swipeToTranslate.direction = UISwipeGestureRecognizerDirectionLeft;
swipeToTranslate.delegate = self;
[cell.messageContentView addGestureRecognizer: swipeToTranslate];

这些是方法......

-(void)tapToTranslate:(UITapGestureRecognizer *)aGesture {}

-(void)translateTo:(UISwipeGestureRecognizer *)aGesture 
{
    aGesture.enabled = false;
}

我已经尝试在调用它的方法后禁用Swipe Gesture但是没有帮助..

我还拥有uigesturerecognizer委托方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

所以任何帮助都会很棒...... 提前谢谢......

编辑1

<UITextView: 0x11322f700; frame = (18 10; 160.865 69.2656); text = '你怎么样?    How are you doing?'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x11322fbd0>; layer = <CALayer: 0x11322fac0>; contentOffset: {0, 0}>

3 个答案:

答案 0 :(得分:1)

UISwipeGestureRecognizer会针对UIGestureRecognizerStateBeganUIGestureRecognizerStateEnded和其他几个州等不同状态多次调用该函数。它还会在刷卡时不断调用该功能,这可以在下面的最后一个语句中处理。在滑动手势功能中,执行以下操作:

-(void)translateTo:(UISwipeGestureRecognizer *)aGesture
{
if (recognizer.state == UIGestureRecognizerStateBegan)
{
//do something
} 
else if(recognizer.state==UIGestureRecognizerStateEnded)
{
}
else
{
 //do something while it is swiping
}
}

以下答案可能与您打算做的事情无关,但仍可能对您有所帮助:UISwipeGestureRecognizer called twice

答案 1 :(得分:1)

从两个手势中删除gesture.delegate = self;行后,该方法开始按原样调用一次。显然是为我做的工作。因此,对于遇到此问题的任何人,他们都可以尝试删除gesture's delegate及其方法。

答案 2 :(得分:0)

问题是,Tableview有自己的手势识别器。通过在tableview上添加额外的手势,它会以某种方式混淆调用手势。这可能是此问题的原因。这是解决方案;

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    if ([gestureRecognizer.view isKindOfClass:[UITableView class]]) {
       ...
    } else {
       ...
    }
}

您可以使用此识别同时手势。