1

当用户将手指按到屏幕上时,我想执行一个连续的动作。这目前工作正常。但是当用户将第二根手指按在屏幕上时,我希望 UILongPressGestureRecognizer 取消/结束第一次按下并开始服从新的按下。这样做的最佳方法是什么?我需要两个吗UILongPressGestureRecognizers,当一个着火时,另一个

enabled=NO;启用=是;

还是有更清洁的方法?

目前,只使用一个UILongPressGestureRecognizer,当第二根手指按下屏幕时,它的行为就像它甚至不知道它在那里一样。

4

2 回答 2

3

UILongPressGestureRecognizer只知道最少的触摸次数——默认情况下,一根手指——然后再放下另一根手指不会有太大的影响。在需要单次触摸的真实手机上尝试此操作会看到初始触摸导致识别器转换到状态 1。将第一根手指向侧面移动或第二根手指向下触摸都会导致转换到状态 2。抬起第一根手指touch down 会导致转换到状态 3 并结束手势。

UILongPressGestureRecognizer在与上面第一个视图相同的视图中添加了第二个,但给这个视图一个最低的两次触摸要求。像这样:

    UILongPressGestureRecognizer *lpgr1 = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpgr1Method:)];
    UILongPressGestureRecognizer *lpgr2 = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpgr2Method:)];
    lpgr2.numberOfTouchesRequired = 2;
    [self.view addGestureRecognizer:lpgr1];
    [self.view addGestureRecognizer:lpgr2];
// ...
- (void)lpgr1Method:(UIGestureRecognizer *)gestureRecognizer {
    UIGestureRecognizerState state = gestureRecognizer.state;
    self.stateLabel.text = [NSString stringWithFormat:@"%d", (int)state];
}
- (void)lpgr2Method:(UIGestureRecognizer *)gestureRecognizer {
    UIGestureRecognizerState state = gestureRecognizer.state;
    self.stateLabel2.text = [NSString stringWithFormat:@"%d", (int)state];
}

如果我首先用一根手指触摸并按住它,lpgr1则进入状态 1。如果我然后用第二根手指触摸并按住它,lpgr1则进入状态 2。lpgr2根本不会触发。

如果我同时用两根手指触碰并按住它,就会lpgr2触发,并且lpgr1永远不会触发。

所以看起来好像在整个视图中添加两个识别器只是令人困惑,并且无论你如何编程都不会达到你想要的结果。UIGestureRecognizer因此,我怀疑正确的方法是编写你自己的子类。

编辑:我也试过这个:

lpgr2.delegate = self;
lpgr1.delegate = self;
// ...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return true;
}

这确实允许两个识别器同时触发,并识别出第二根手指已经触地。但是,lpgr1一旦移除第一根手指,就会结束。我也无法做到你想要的。我会写子类,但我的注意力需要在其他地方。祝你好运!

于 2012-12-23T10:08:07.707 回答
-1

我刚刚制作了一个新的手势识别器类。它很简单,并且适用于 1 或 2 次触摸的连续动作。

FCtwoTouchesRecognizer.h

#import <UIKit/UIKit.h>
@interface FCTwoTouchesRecognizer : UIGestureRecognizer
@end

FCtwoTouchesRecognizer.m

#import "FCTwoTouchesRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>

@implementation FCTwoTouchesRecognizer
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    if ([touches count] > 2) //only 1 or 2 touches
    {
        self.state = UIGestureRecognizerStateFailed;
        return;
    }
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    if (self.state == UIGestureRecognizerStateFailed) return;
    self.state = UIGestureRecognizerStateChanged;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    if ( (self.numberOfTouches - [touches count]) == 0 )
        self.state = UIGestureRecognizerStateRecognized;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
    self.state = UIGestureRecognizerStateFailed;
}
@end

用法:

1) 添加

FCTwoTouchesRecognizer *recog = [[FCTwoTouchesRecognizer alloc] initWithTarget:self action:@selector(twoTouchesGestureHandler:)];
        [self addGestureRecognizer:recog];

2) 处理

-(void)twoTouchesGestureHandler:(FCTwoTouchesRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateChanged)
    {
        NSLog(@"changed");
        if ([recognizer numberOfTouches] > 1)
        {
            CGPoint point1 = [recognizer locationOfTouch:0 inView:self];
            CGPoint point2 = [recognizer locationOfTouch:1 inView:self];
        }
        else
        {
            CGPoint point = [recognizer locationInView:self];
        }
    }

    if(recognizer.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"touches ended");
    }
}
于 2014-02-06T10:58:21.857 回答