如何检测 UISwipeGestureRecognizer 的结束?

Posted

技术标签:

【中文标题】如何检测 UISwipeGestureRecognizer 的结束?【英文标题】:How to detect end of UISwipeGestureRecognizer? 【发布时间】:2013-11-10 23:06:23 【问题描述】:

来自 Apple 文档

滑动是一种离散的手势,因此每个手势只发送一次相关的动作消息。

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event 

当我使用 UISwipeGestureRecognizer 时也不会被调用

如何检测用户何时抬起手指?

【问题讨论】:

Detect when the finger lifts after a UISwipeGesture[Recognizer] 的可能副本。你想要这个问题的不被接受的答案。 【参考方案1】:

我想通了,实际上这很容易,而不是使用 UISwipeGestureRecognizer 来检测滑动我使用事件处理自己检测到它

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    UITouch *touch = [touches anyObject];
    self.initialPosition = [touch locationInView:self.view];

 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
     UITouch *touch = [touches anyObject];
     CGPoint movingPoint = [touch locationInView:self.view];
     CGFloat moveAmt = movingPoint.y - self.initialPosition.y;
     if (moveAmt < -(minimum_detect_distance)) 
       [self handleSwipeUp];
      else if (moveAmt > minimum_detect_distance) 
       [self handleSwipeDown];
     
  
 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
      [self reset];

  
 -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
     [self reset];
  

我没有对 UIGestureRecognizer 进行子类化,而是仅在所需的视图控制器中进行事件处理,因为在重置方法中,我正在重置属于视图控制器的几个变量、计数器和计时器。

【讨论】:

【参考方案2】:

我认为最好检查一下手势识别器的 state 属性:

- (void)swipe:(UISwipeGestureRecognizer *)recognizer

   CGPoint point = [recognizer locationInView:[recognizer view]];
   if (recognizer.state == UIGestureRecognizerStateBegan)
       NSLog(@"Swipe began");
   else if (recognizer.state == UIGestureRecognizerStateEnded)
       NSLog(@"Swipe ended");

【讨论】:

是的,我之前尝试过,但 UISwipeGestureRecognizer 是离散手势而不是连续手势,因此只有被调用的状态是 UIGestureRecognizerStateEnded,因此没有用。【参考方案3】:

UISwipeGestureRecognizer 是 Apple 文档中所说的离散手势,因此您需要使用连续手势,在这种情况下使用 UIPanGestureRecognizer。

代码如下:

- (void)viewDidLoad
[super viewDidLoad];

// add pan recognizer to the view when initialized
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognized:)];
[panRecognizer setDelegate:self];
[yourView addGestureRecognizer:panRecognizer]; // add to the view you want to detect swipe on



-(void)panRecognized:(UIPanGestureRecognizer *)sender

CGPoint distance = [sender translationInView: yourView];


if (sender.state == UIGestureRecognizerStateEnded) 
    [sender cancelsTouchesInView];


    if (distance.x > 70 && distance.y > -50 && distance.y < 50)  // right

        NSLog(@"user swiped right");
        NSLog(@"distance.x - %f", distance.x);

     else if (distance.x < -70 && distance.y > -50 && distance.y < 50)  //left

        NSLog(@"user swiped left");
        NSLog(@"distance.x - %f", distance.x);

    

    if (distance.y > 0)  // down
        NSLog(@"user swiped down");
        NSLog(@"distance.y - %f", distance.y);
     else if (distance.y < 0)  //up
        NSLog(@"user swiped up");
        NSLog(@"distance.y - %f", distance.y);
    



别忘了添加 UIGestureRecognizerDelegate。

【讨论】:

【参考方案4】:

如果你使用scrollView,你可以检测到它的contentOffset

func scrollViewDidScroll(_ scrollView: UIScrollView) 
    if scrollView.contentOffset.y < -100  // how you needed
        // do what you need
    

【讨论】:

以上是关于如何检测 UISwipeGestureRecognizer 的结束?的主要内容,如果未能解决你的问题,请参考以下文章

如何检测 SD 卡何时插入?

如何在张量流对象检测中仅检测特定类别的对象

如何使用 TextRecognizer 检测单词?它只能检测 TextBlocks

如何快速入门深度学习目标检测?

如何计算 OpenCV 人脸检测器的检测分数?

业务系统弱口令如何检测