为什么UIPanGestureRecognizer会多次触发UIGestureRecognizerStateEnded?

时间:2016-08-05 14:26:47

标签: ios uipangesturerecognizer

我想要实现的只是在用户开始手势的点和他结束这样做的点之间划一条线。我认为UIGestureRecognizerStateEnded是我需要的状态,但它被多次调用。如果有人能解释我为什么会这样,以及如何抓住最后一点,我将非常感激。

- (void)drawingViewDidPan:(UIPanGestureRecognizer*)sender
{
CGPoint currentDraggingPosition = [sender locationInView:_drawingView];

if(sender.state == UIGestureRecognizerStateBegan){
    _prevDraggingPosition = currentDraggingPosition;
    NSLog(@"---");
}

if(sender.state != UIGestureRecognizerStateEnded){
    [self drawLine:_prevDraggingPosition to:currentDraggingPosition];
     NSLog(@"???");
}
_prevDraggingPosition = currentDraggingPosition;
}

日志:

  

2016-08-05 17:14:46.0​​86 X [2518:356899] ---   2016-08-05 17:14:46.0​​92 X [2518:356899] ???   2016-08-05 17:14:46.127 X [2518:356899] ???   2016-08-05 17:14:46.153 X [2518:356899] ???   2016-08-05 17:14:46.177 X [2518:356899] ???   2016-08-05 17:14:46.205 X [2518:356899] ???   2016-08-05 17:14:46.226 X [2518:356899] ???   2016-08-05 17:14:46.246 X [2518:356899] ???   2016-08-05 17:14:46.279 X [2518:356899] ???   ...

1 个答案:

答案 0 :(得分:1)

if(sender.state != UIGestureRecognizerStateEnded){
    [self drawLine:_prevDraggingPosition to:currentDraggingPosition];
     NSLog(@"???");
}

sender.state != UIGestureRecognizerStateEnded将成功评估除UIGestureRecognizerStateEnded之外的每种类型的手势状态。

!=更改为==,它将正常运作。