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

来自 Apple 文档

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

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

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

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

【问题讨论】:

标签: ios iphone objective-c uigesturerecognizer


【解决方案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
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-06-22
        • 1970-01-01
        • 2012-10-30
        • 1970-01-01
        • 1970-01-01
        • 2012-09-17
        • 1970-01-01
        • 2011-05-08
        相关资源
        最近更新 更多