有时候我们在使用长按这个手势触发事件的时候,发现我们的事件触发了两次,其实很容易理解,这两次是对应这个手势不同的状态而触发的,一次是手势开始时,一次是手势结束时,我们在触发事件的时候加上一个判断就可以解决这个问题。
下面我们来看一下代码:
<span style="font-family:Arial;font-size:18px;">UILongPressGestureRecognizer *longGestureRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
longGestureRecognizer.allowableMovement = 30;
longGestureRecognizer.delegate = self;
[self.view addGestureRecognizer:longGestureRecognizer];</span>
<span style="font-family:Arial;font-size:18px;">-(void)handleLongPress:(UILongPressGestureRecognizer *)recognizer
{
//添加该判断可以防止长按后触发多次事件
if (recognizer.state == UIGestureRecognizerStateBegan){
[self showBottomMenu];
}
}</span>
if (recognizer.state == UIGestureRecognizerStateBegan)
我们在触发事件的时候加上这个判断之后问题就引刃而解啦!