为什么多次调用gestureRecognizerShouldBegin?

时间:2016-11-06 11:01:20

标签: ios objective-c gesture

我做了这样的手势:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]     
initWithTarget:self action:@selector(tapOnPhoto:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;                                              
tapGesture.delegate = self;
[self.htmlWebView addGestureRecognizer:tapGesture]; 

点击 htmlWebView 多次调用 gestureRecognizerShouldBegin

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]){
        [self performSelector:@selector(tapOnPhoto:) withObject:gestureRecognizer];

    }
    return YES;
}

为什么每个手势多次调用gestureRecognizerShouldBegin?

2 个答案:

答案 0 :(得分:0)

按照 gestureRecognizerShouldBegin说明。

  

当手势识别器尝试转换时,会调用此方法   超出UIGestureRecognizerStatePossible状态。返回NO导致   手势识别器转换到   UIGestureRecognizerStateFailed状态。

此方法将被多次调用以获得手势的过渡状态。

此外,当您为TapGesture添加了一个选择器时,为什么要显式调用该方法,该方法将自行调用。

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]){
        [self performSelector:@selector(tapOnPhoto:) withObject:gestureRecognizer];

    }
    return YES;
}

不需要这个功能你可以删除它,因为你只调用那里的方法。

答案 1 :(得分:0)

正如@Rajat在他的回答中提到的,您可以删除委托方法,而是使用tapOnPhoto:方法来解析您的手势逻辑。

作为该方法的参数,您将收到的UIGestureRecognizer对象具有名为state的属性

可能具有以下值:

UIGestureRecognizerStatePossible
UIGestureRecognizerStateBegan
UIGestureRecognizerStateChanged
UIGestureRecognizerStateEnded
UIGestureRecognizerStateCancelled
UIGestureRecognizerStateFailed
UIGestureRecognizerStateRecognized

您可以使用switch并为每种情况提供特定的逻辑。