2

我试图在我的 ViewController(它有一个 tableView)中实现两个手势识别器,它们必须一个接一个地工作。第一个是向下滑动手势,第二个是长按手势。

这是我用@sergio 建议修改的代码

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        swipeDown = [[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDownAction)] autorelease];

        longPress = [[[CustomLongPress alloc]initWithTarget:self action:@selector(longPressAction)] autorelease];


        longPress.minimumPressDuration = 2;

        swipeDown.numberOfTouchesRequired = 1;

        swipeDown.direction = UISwipeGestureRecognizerDirectionDown;


       swipeDown.delegate = self ;

        longPress.delegate = self ;

        [myTableView addGestureRecognizer:swipeDown];

        [myTableView addGestureRecognizer:longPress];





  }


    -(void)swipeDownAction {

        _methodHasBeenCalled = YES;    // bool @property declared in .h

        NSLog(@"Swipe down detected");


    }



    -(void)longPressAction {

        NSLog(@"long press detected");
    }

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

还有我的 UILongPressGestureRecognizer 子类:

#import "CustomLongPress.h"
#import "ViewController.h"


@interface CustomLongPress()
{

    ViewController *vc;
}

@end


@implementation CustomLongPress
-(id)initWithTarget:(id)target action:(SEL)action controller:(ViewController *)viewCon
{
    self = [super initWithTarget:target action:action];

    if (self) {

        vc = viewCon;

    }

    return self;
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     NSLog(vc.methodHasBeenCalled ? @"Yes" : @"No");

    if (vc.methodHasBeenCalled) {

        [super touchesBegan:touches withEvent:event];
    }
}

不幸的是,我仍然只从 swipeDown 获得日志,但在 longPress 方面没有日志

4

2 回答 2

1

为此,您需要创建自己的自定义手势识别器。最好的方法是让您子类UILongPressGestureRecognizer化并使其仅在滑动结束后“接受”长按。例如,在touchesBegan方法中

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
   <if swipe recognizer action method has been called>
       [super touchesBegan:touches withEvent:event];
   }
}

这样,两个手势识别器都会尝试识别手势:滑动识别器会立即这样做;而自定义识别器将“等待”滑动识别器触发。

实现条件的一种简单方法<if swipe recognizer action method has been called>是在您的滑动操作中设置一个全局标志(您在执行滑动操作时设置它;然后自定义识别器读取它的值)。这很容易,到目前为止还不是你能找到的最好的实现。

另一种方法是依赖requiresGestureRecognizerToFail链接 3 个标准手势识别器,我们称它们为 A、B 和 C,其中:

  1. A 是一个UISwipeGestureRecognizer
  2. C是一个UILongPressGestureRecognizer
  3. B 是任何手势识别器。

您可以像这样配置它们:

C --> B --> A

借此x --> y表示x需要y失败。因此,您将拥有C(您的长按手势识别器)将要求BGR 失败并B要求A(您的滑动识别器)失败;B一旦A识别到滑动就会失败;一旦B失败,C将允许识别长按,如果有的话。

编辑:

阅读您的评论后,您是否介意尝试一下,看看是否有帮助:

  1. 删除您的覆盖touchesBegan并将其替换为:

    - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    }
    
  2. 然后定义:

    - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    
      if (vc.methodHasBeenCalled) {
        [super touchesBegan:touches withEvent:event];
      } else {
         return;
      }
    }
    

如果这不起作用,则让您的自定义手势识别器继承自通用手势识别器,并

  1. touchesBegan:在原地并替换touchesMoved为:

    - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    
      self.lastTouch = [touches anyObject];
      if (vc.methodHasBeenCalled && self.gestureNotBegunYet == YES) {
        self.gestureNotBegunYet = NO;
        [self performSelector:@selector(recognizeLongPress) withObject:nil afterDelay:1.0];
    
      } else {
         return;
      }
    }
    

并添加:

- (float)travelledDistance {
  CGPoint currentLocation = [self.lastTouch locationInView:self.view.superview];
  return sqrt(pow((currentLocation.x - self.initialLocation.x), 2.0) +
            pow((currentLocation.y - self.initialLocation.y), 2.0));
}

- (void)fail {
  self.gestureNotBegunYet = YES;
  [NSObject cancelPreviousPerformRequestsWithTarget:self];

}

- (void)recognizeLongPress {

  if ([self travelledDistance] < kTapDragThreshold) {
    self.longPressed = YES;
    self.state = UIGestureRecognizerStateChanged;
  } else {
        [self fail];
        self.state = UIGestureRecognizerStateFailed;
  }
}

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
  [self fail];
  [super touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
  [self fail];
  [super touchesCancelled:touches withEvent:event];
}

您将需要在.h

@property(nonatomic) CGPoint initialLocation;
@property(nonatomic, retain) UITouch* lastTouch;
@property(nonatomic) BOOL gestureNotBegunYet;
于 2012-12-05T15:41:43.177 回答
0

如果我没记错的话,一旦系统检测到特定类型的手势(点击、滑动、平移、长按等),它就不能再为该触摸动作发送不同类型的手势。

这并不妨碍您实现您想要的结果。也许结合重载以下UIResponder方法:

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:

并且使用手势识别器(不确定输入是否会被一个消耗而另一个忽略),您可以确定它是“长滑动”

于 2012-12-05T15:40:09.713 回答