【问题标题】:Long Press Gesture Recognizer Fired Two times [duplicate]长按手势识别器触发两次[重复]
【发布时间】:2015-08-28 03:51:50
【问题描述】:

我对 LongPressGestureRecognizer 感到困惑。我将其中一个放在滚动视图上,但它工作了两次。当我抬起手指时,添加的方法再次调用。我想知道它只是第一次调用。应该我愿意吗?任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 显示创建和应用手势识别器的代码以及识别器处理程序的代码。
  • 是的,当你开始印刷时它有两个叫一个,当它结束时另一个叫

标签: ios objective-c long-press


【解决方案1】:

先看看苹果文档是怎么说的:-

“长按手势是连续的。当允许的手指数(numberOfTouchesRequired)被按下指定的时间(minimumPressDuration)并且触摸没有超出允许的移动范围(allowableMovement)时,手势开始(UIGestureRecognizerStateBegan) )。只要手指移动,手势识别器就会转换到 Change 状态,并在任何手指抬起时结束 (UIGestureRecognizerStateEnded)。"

 -  (void)LongPress:(UILongPressGestureRecognizer*)sender { 

        if (sender.state == UIGestureRecognizerStateBegan){
           NSLog(@"UIGestureRecognizerStateBegan.");
       //in your case add your functionality over here
         }
        else if (sender.state == UIGestureRecognizerStateEnded) {
          NSLog(@"UIGestureRecognizerStateEnded");
        //if you want to add some more functionality when gesture got ended.

         }

      }

【讨论】:

    【解决方案2】:

    UILongPressGestureRecognizer 与 UITapGestureRecognizer 不同。它包含一些状态。

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2);
        [self.view addSubview:scrollView];
    
        UILongPressGestureRecognizer *lpGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lpHandler:)];
        [scrollView addGestureRecognizer:lpGes];
    }
    
    - (void)lpHandler:(UILongPressGestureRecognizer *)lpGes
    {
        switch (lpGes.state) {
            case UIGestureRecognizerStateBegan:
                NSLog(@"UILongPressGestureRecognizer: began");
                break;
    
            case UIGestureRecognizerStateEnded:
                NSLog(@"UILongPressGestureRecognizer: ended");
                break;
    
            default:
                break;
        }
    }
    

    对于上述代码,您将获得 2 个日志:

    2015-08-28 12:22:39.084 aaaaa[50704:2339282] UILongPressGestureRecognizer: began
    2015-08-28 12:22:40.687 aaaaa[50704:2339282] UILongPressGestureRecognizer: ended
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-23
      相关资源
      最近更新 更多