手势问题:UISwipeGestureRecognizer + UI滑块

发布于 2024-10-13 13:10:46 字数 1346 浏览 5 评论 0原文

遇到与手势相关的问题。我实现了 UISwipeGestureRecognizer 来获取向左和向右滑动事件,并且工作正常。然而,我面临的问题是,我在同一视图中的 UISlider 玩得不好。滑块的滑动动作被误认为是向左/向右滑动。

任何人以前遇到过这个问题,有任何想法如何纠正它吗?

非常感谢。

这是视图控制器中包含的代码:

 - (void)viewDidLoad {

            [super viewDidLoad];

                //Setup handling of LEFT and RIGHT swipes
             UISwipeGestureRecognizer *recognizer;

                recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
                [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
                [[self view] addGestureRecognizer:recognizer];
                [recognizer release];

                recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
                [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
                [[self view] addGestureRecognizer:recognizer];
                [recognizer release];
        }

    -(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {

      if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
       NSLog(@"Swipe Right");
       //Do stuff
      }

      if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
       NSLog(@"Swipe Left");
       //Do stuff
      }
    }

Got a gesture related problem. I implemented UISwipeGestureRecognizer to get swipe left and right events and that is working fine. However the problem I'm facing is that the UISlider's I have in the same view are not playing nice. The sliding motion of the sliders is being mistaken as a swipe left/right.

Any one experienced this problem before, got any ideas how to correct it?

Many thanks.

Here is the code contained within the view controller:

 - (void)viewDidLoad {

            [super viewDidLoad];

                //Setup handling of LEFT and RIGHT swipes
             UISwipeGestureRecognizer *recognizer;

                recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
                [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
                [[self view] addGestureRecognizer:recognizer];
                [recognizer release];

                recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
                [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
                [[self view] addGestureRecognizer:recognizer];
                [recognizer release];
        }

    -(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {

      if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
       NSLog(@"Swipe Right");
       //Do stuff
      }

      if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
       NSLog(@"Swipe Left");
       //Do stuff
      }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夕色琉璃 2024-10-20 13:10:46

处理此问题的最简单方法可能是防止手势识别器看到滑块上的触摸。您可以通过将自己设置为委托来做到这一点,然后实现

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isKindOfClass:[UISlider class]]) {
        // prevent recognizing touches on the slider
        return NO;
    }
    return YES;
}

如果这不起作用,则滑块可能实际上具有接收触摸的子视图,因此您可以沿着 superview 链向上走,测试沿途的每一个景色。

The simplest way to handle this is probably to prevent the gesture recognizer from seeing touches on your slider. You can do that by setting yourself as the delegate, and then implementing

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isKindOfClass:[UISlider class]]) {
        // prevent recognizing touches on the slider
        return NO;
    }
    return YES;
}

If this doesn't work, it's possible the slider actually has subviews that receive the touch, so you could walk up the superview chain, testing each view along the way.

〆凄凉。 2024-10-20 13:10:46

斯威夫特 4.0 版本。 不要忘记 UIGestureRecognizerDelegate。

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {

    if let touchedView = touch.view, touchedView.isKind(of: UISlider.self) {
        return false
    }

    return true
}

Swift 4.0 version. Don't forget the UIGestureRecognizerDelegate.

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {

    if let touchedView = touch.view, touchedView.isKind(of: UISlider.self) {
        return false
    }

    return true
}
锦上情书 2024-10-20 13:10:46

在莉莉做出上述回应之前,我最终完成了这项工作。这是我使用的代码,但莉莉的看起来更干净(你还没有测试莉莉的):

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    BOOL AllowSwipes = YES;

        CGPoint point1 = [touch locationInView:_sliderLeft];
        CGPoint point2 = [touch locationInView:_sliderRight];

        //Left slider test
        if ([_sliderLeft hitTest:point1 withEvent:nil] != nil) {
            AllowSwipes = NO;
            NSLog(@"On Left Slider");
        }

        //Right slider test
        if ([_sliderRight hitTest:point2 withEvent:nil] != nil) {
            AllowSwipes = NO;
            NSLog(@"On Right Slider");
        }
    }
    return AllowSwipes;
}

I ended up getting this working just before Lily responded above. Here is the code I used, but Lily's looks cleaner (haven't tested Lily's thou):

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    BOOL AllowSwipes = YES;

        CGPoint point1 = [touch locationInView:_sliderLeft];
        CGPoint point2 = [touch locationInView:_sliderRight];

        //Left slider test
        if ([_sliderLeft hitTest:point1 withEvent:nil] != nil) {
            AllowSwipes = NO;
            NSLog(@"On Left Slider");
        }

        //Right slider test
        if ([_sliderRight hitTest:point2 withEvent:nil] != nil) {
            AllowSwipes = NO;
            NSLog(@"On Right Slider");
        }
    }
    return AllowSwipes;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文