滑动手势识别器不会触发?

时间:2015-05-12 22:04:30

标签: ios objective-c xcode swipe

我有一个页面视图控制器,其中显示了五个视图控制器。

我尝试添加这些滑动手势但是没有被调用?

   UISwipeGestureRecognizer *swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)];
swipeGestureRight.numberOfTouchesRequired = 1;
swipeGestureRight.direction = (UISwipeGestureRecognizerDirectionRight);
[self.scrollView addGestureRecognizer:swipeGestureRight];

UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)];
swipeGestureLeft.numberOfTouchesRequired = 1;
swipeGestureLeft.direction = (UISwipeGestureRecognizerDirectionLeft);
[self.scrollView addGestureRecognizer:swipeGestureLeft];

为什么这些被触发,我该怎么办呢?我的目标是检测用户滑动页面视图控制器的方向。

2 个答案:

答案 0 :(得分:0)

向self.view添加手势

[self.view addGestureRecognizer:swipeGestureRight];

我希望它会起作用

答案 1 :(得分:0)

试试这个

  • 设置UIGestureRecognizer

  • 的代理人
  • 实施shouldRecognizeSimultaneouslyWithGestureRecognizer

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
    }
    
  • 实施shouldReceiveTouch

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
     return YES;
    }
    

希望这会有所帮助。如果你仍然面临这个问题,请告诉我。