如何在iOS中处理1到3个手指的滑动手势

发布于 2024-12-29 17:42:34 字数 941 浏览 1 评论 0原文

我在代码中使用以下代码来处理 1 根手指滑动:

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewsSwipe:)];
    [swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
    [swipe setDelaysTouchesBegan:YES];
    [[self view] addGestureRecognizer:swipe];

我知道我可以添加以下行以使其处理 2 根手指滑动:

 [swipe setNumberOfTouchesRequired:2];

但是,当我添加上述代码时,由于所需的触摸次数,不再检测到 1 根手指滑动现在是 2。我该怎么做才能使我的代码适用于 1、2 或 3 根手指的滑动?

我尝试使用以下代码,但这并没有达到我想要的效果。

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewsSwipe:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:3];
    [panRecognizer setDelaysTouchesBegan:YES];
    [[self view] addGestureRecognizer:panRecognizer];
    [panRecognizer release];

谢谢。

I use the following code to handle 1 finger swipe in my code:

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewsSwipe:)];
    [swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
    [swipe setDelaysTouchesBegan:YES];
    [[self view] addGestureRecognizer:swipe];

I know i can add the following line to make it handle 2 fingers swipe:

 [swipe setNumberOfTouchesRequired:2];

However when I add the above code 1 finger swipe is no longer detected since the number of touches required is now 2. What can I do to make my code work for 1, 2 or 3 fingers swipe?

I tried using the following code but this doesn't do what I want to do.

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewsSwipe:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:3];
    [panRecognizer setDelaysTouchesBegan:YES];
    [[self view] addGestureRecognizer:panRecognizer];
    [panRecognizer release];

Thank you.

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

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

发布评论

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

评论(2

你怎么这么可爱啊 2025-01-05 17:42:34

在你的handleViewsSwipe中,你可以从手势识别器获取numberOfTouches属性。

- (void)handleViewsSwipe:(UISwipeGestureRecognizer *)recognizer {
    NSUInteger touches = recognizer.numberOfTouches;
    switch (touches) {
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        default:
            break;
    }
}

只需根据您获得的触摸次数切换相同的方法来执行操作即可。

In your handleViewsSwipe you can get the numberOfTouches property from the gesture recognizer.

- (void)handleViewsSwipe:(UISwipeGestureRecognizer *)recognizer {
    NSUInteger touches = recognizer.numberOfTouches;
    switch (touches) {
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        default:
            break;
    }
}

Just switch the same method for what to do depending on how many touches you get.

知足的幸福 2025-01-05 17:42:34

将三个滑动手势识别器添加到您的视图中:

for (int i = 1; i <= 3; ++i) {
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewsSwipe:)];
    swipe.numberOfTouchesRequired = i;
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    swipe.delaysTouchesBegan = YES;
    [self.view addGestureRecognizer:swipe];
}

对我有用。

Add three swipe gesture recognizers to your view:

for (int i = 1; i <= 3; ++i) {
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewsSwipe:)];
    swipe.numberOfTouchesRequired = i;
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    swipe.delaysTouchesBegan = YES;
    [self.view addGestureRecognizer:swipe];
}

Worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文