简体   繁体   English

滑动手势多次调用该方法

[英]Swipe Gesture calling the method multiple times

I have a small problem that em stuck on.. i have a Custom UITableViewCell , on its textView i have added 2 gestures , UITapGesture and UISwipeGesture .. the tap gesture is working fine but the swipe gesture is calling the method multiple times .. some times calling it twice and some times even more than that… Here's how i have added them to the cell 我有一个小问题,即EM卡上..我有一个自定义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];

These are there methods… 这些是方法...

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

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

I've tried to disable Swipe Gesture in its method after its called but that didn't help.. 我试图在调用后禁用其方法中的“滑动手势”,但这无济于事。

I've also have the uigesturerecognizer delegate method 我也有uigesturerecognizer委托方法

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

So Any help would be great… thanks in advance… 因此,任何帮助都是很棒的…预先感谢…

EDIT 1 编辑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}>

The UISwipeGestureRecognizer does call the function multiple times for different states like UIGestureRecognizerStateBegan , UIGestureRecognizerStateEnded , and several other states. UISwipeGestureRecognizer确实针对不同状态(例如UIGestureRecognizerStateBeganUIGestureRecognizerStateEnded和其他几种状态)多次调用了该函数。 It also keeps on calling the function constantly while it is swiping which can be handled in the last else statement below. 它还在刷卡时不断调用该函数,可以在下面的最后else语句中进行处理。 In the swipe gesture function, do this: 在滑动手势功能中,执行以下操作:

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

The below answer may not be in correspondence to what you intend to do but still might help you: UISwipeGestureRecognizer called twice 下面的答案可能与您要执行的操作不符,但仍可能会帮助您: UISwipeGestureRecognizer调用了两次

After Removing the gesture.delegate = self; 删除gesture.delegate = self;之后gesture.delegate = self; line from both the gestures, the method started calling once as it was suppose to do. 从这两个手势中选择一行,该方法便按预期方式开始调用一次。 Apparently doing the job for me. 显然是为我做这份工作。 So for anyone facing this issue, they can try removing the gesture's delegate and its method. 因此,对于任何面临此问题的人,他们都可以尝试删除gesture's delegate及其方法。

The thing is, Tableview is having its own gesture recognizers. 事实是,Tableview具有自己的手势识别器。 By adding additional gestures over the tableview it is somehow confusing with the gesture to call. 通过在表视图上添加其他手势,将调用手势混淆了。 This might be the reason of this issue. 这可能是此问题的原因。 Here is the solution; 这是解决方案;

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

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

You can recognize simultaneous gestures using this. 您可以使用此识别同时手势。

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

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