滑动手势识别器无法检测方向?

Swipe Gesture Recognizer can't detect the direction?

如果我像这样使用手势识别器

 UISwipeGestureRecognizer *noRightShiftForVolume = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleVolumeViewSwipe1:)];
   [self.circleProgressBar addGestureRecognizer:noRightShiftForVolume];

因为我在 ViewController 上有导航控制器,所以这不会禁用视图上的滑动。 但是当我这样声明时它工作正常但不知道滑动的方向。

UIPanGestureRecognizer *noRightShiftForVolume = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleVolumeViewSwipe1:)];
    [self.circleProgressBar addGestureRecognizer:noRightShiftForVolume];

函数的实现是这样的:

- (void)handleVolumeViewSwipe1:(UISwipeGestureRecognizer *)sender
{

    if (sender.state == UIGestureRecognizerStateBegan) {
        [applicationControllerShared() enableSwipe:NO];
        NSLog(@"good karens");

        if(UISwipeGestureRecognizerDirectionLeft){
        CATransition *animation = [CATransition animation];
        [animation setType:kCATransitionPush];
        [animation setSubtype:kCATransitionFromRight];
        [animation setDuration:0.50];
        [animation setTimingFunction:
         [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [_circleProgressBar.layer addAnimation:animation forKey:kCATransition];
        NSLog(@"Swipe Left");
        }

        if(UISwipeGestureRecognizerDirectionRight){
            NSLog(@"Swipe Right");
        }
    }

    if (sender.state == UIGestureRecognizerStateEnded)
    {
        [applicationControllerShared() enableSwipe:YES];
        NSLog(@"good karens 110");
    }
}

在导航控制器中 BOOL 消除刷卡; eliminateSwipe 函数如下所示:

-(void) enableSwipe: (BOOL) state
{
    eliminateSwipe = !state;
}

启用滑动功能禁用导航控制器滑动并使我能够实现函数 handleVolumeViewSwipe1。 我有圆形进度条,我想检测左右滑动并根据其执行功能。知道我该怎么做吗?

我已尝试在 ViewDidLoad 和 ViewDidAppear 中实现此 SwipeGestureRecognizerDirection,但由于导航控制器的滑动功能而对我不起作用。

这在 UIPanGestureRecognizer 的帮助下解决了,可以使用以下代码检测滑动

  if (sender.state == UIGestureRecognizerStateBegan) {
        [applicationControllerShared() enableSwipe:NO];
        CGPoint velocity = [sender velocityInView:_circleProgressBar];

        if(velocity.x < 0)
        {
          NSLOG(@"swipe left");
}else{
 NSLOG(@"swipe right");}
}

根据我的经验,您需要应用 UINavigationControllerDelegate 的委托来停止 popGesture。

第 1 步:在 .h 文件中确认协议。(可选步骤)

@interface ViewController : UIViewController<UINavigationControllerDelegate>

步骤 2:在 viewDidLoad. 中设置委托(可选步骤)

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

第 3 步:并在之前的 viewWillDisApper 中禁用手势 VC。(必需步骤)

self.navigationController.interactivePopGestureRecognizer.enabled = YES;

希望对您有所帮助。