3

来自 Apple 文档

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

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

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

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

4

4 回答 4

3

我想通了,实际上这很容易,而不是使用 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 而是仅在所需的视图控制器中进行事件处理,因为在重置方法中我正在重置属于视图控制器的几个变量、计数器和计时器。

于 2013-11-11T04:22:34.787 回答
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");
}
于 2013-11-11T04:27:49.393 回答
1

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。

于 2015-07-01T17:21:03.117 回答
0

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

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y < -100 { // how you needed
        // do what you need
    }
}
于 2019-08-26T19:16:25.747 回答