【问题标题】:UILongPressGestureRecognizer - Only recognize last pressUILongPressGestureRecognizer - 只识别最后一次按下
【发布时间】:2012-12-10 02:52:21
【问题描述】:

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

enabled=NO;enabled=YES;

或者有更清洁的方法吗?

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

【问题讨论】:

    标签: iphone ios xcode ipad cocoa-touch


    【解决方案1】:

    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 会在第一根手指移开后立即结束。我也无法做到你想要的。我会写子类,但我的注意力需要在其他地方。祝你好运!

    【讨论】:

    • 几乎证实了我的恐惧。谢谢:)
    【解决方案2】:

    我刚刚创建了一个新的手势识别器类。它很简单,并且对于 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");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多