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

时间:2017-01-27 13:02:36

标签: ios objective-c uigesturerecognizer

如果我正在使用这样的手势识别器

 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");}
}

1 个答案:

答案 0 :(得分:0)

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

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

@interface ViewController : UIViewController<UINavigationControllerDelegate>

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

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

步骤3:在previos VC的viewWillDisApper中停用手势。(必填步骤)

self.navigationController.interactivePopGestureRecognizer.enabled = YES;

希望这会对你有所帮助。