2

我有一个分段控件,允许短手势和长手势。短手势识别很好。长手势方法被调用了两次。我在挠头,为什么。

这是构建颜色工具栏的代码的一部分:

UILongPressGestureRecognizer* longPressGestureRec =
    [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    longPressGestureRec.minimumPressDuration = 1.5;
    //longPressGestureRec.cancelsTouchesInView = NO;
    [colorControl addGestureRecognizer:longPressGestureRec];

这是 longPress 方法的一部分:

-(void) longPress:(id)sender {
    NSLog(@"%s", __FUNCTION__);     
    switch (colorIndex) {
        case 0:
            [self showMoreWhiteColors:(id)sender];
            break;

        case 1:
            [self showMoreRedColors:(id)sender];
            break;

通过查看日志,我可以看到每次按住按钮时都会调用两次 longPress 方法。

任何想法我做错了什么,失踪,没有做......?

4

3 回答 3

7

我只是检查状态是否除了 UIGestureRecognizerStateBegan 之外的任何东西,然后在执行我想要的代码之前返回。所以:

-(void) longPressGesture:(UIGestureRecognizer*)gesture
{
    if ( gesture.state != UIGestureRecognizerStateBegan )
       return; // discard everything else

   // do something in response to long gesture
}
于 2012-04-27T02:12:37.237 回答
3
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
if(UIGestureRecognizerStateBegan == gesture.state) {
    // Called on start of gesture, do work here
}

if(UIGestureRecognizerStateChanged == gesture.state) {
    // Do repeated work here (repeats continuously) while finger is down
}

if(UIGestureRecognizerStateEnded == gesture.state) {
    // Do end work here when finger is lifted
}

}

于 2015-01-28T18:07:48.153 回答
1

或者你可以这样做。

-(void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
      switch(gesture.state){
       case UIGestureRecognizerStateBegan:

            // Do your stuff here.
            NSLog(@"State Began");
            break;
       case UIGestureRecognizerStateChanged:
            NSLog(@"State changed");
            break;
       case UIGestureRecognizerStateEnd:
            NSLog(@"State End");
            break;
       default:
            break;
      }
}
于 2014-07-04T08:26:59.650 回答