UIGestureRecognizer 滑动并按住

发布于 2024-10-31 08:09:51 字数 1504 浏览 5 评论 0原文

我正在尝试实现滚动菜单的效果。如果用户进行单次滑动(向任何方向),我将为每次滑动移动一个项目。使用 UISwipeGestureReconizer 效果很好,但是如果用户在滑动后继续按住屏幕上的手指,我想继续滚动直到手指抬起。

看来我想将 UISwipeGestureRecognizer 与 UILongPressGestureRecognizer 结合起来来完成此任务。成功滑动 (UISwipeGestureRecognizer) 后,启用 UILongPressGestureRecognizer 并开始滚动,直到手指抬起(UILongPressGestureRecognizer 结束)。

问题是成功滑动后不会发生 UILongPressGestureRecognizer 事件。

下面是一个示例:

// Create single swipe recognizer to recognize right swipes
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleSwipeFrom:)];
swipeRecognizer.delegate = self;
[_gestureView addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];

// Create long press recognizer to recognize right press and holds
longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeAndHoldFrom:)];
longPressRecognizer.delegate = self;
[longPressRecognizer setMinimumPressDuration:0.5];
[longPressRecognizer setEnabled:NO];
[_gestureView addGestureRecognizer:longPressRecognizer];
[longPressRecognizer release];

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

    if ([recognizer state] == UIGestureRecognizerStateEnded) {
        // Move one item in direction based on recognizer.direction
        [_longPressGestureRecognizer setEnabled:YES];
    }
}

如果我在滑动后将手指放在屏幕上,则永远不会调用 UILongPressGestureRecognizer 事件。关于如何实现这一目标有什么想法吗?

I am trying to implement the effect of scrolling through a menu. If the user does a single swipe (in any direction) I will move one item for each swipe. This is working just fine using UISwipeGestureReconizer, but if the user keeps holding there finger down on the screen after the swipe I want to keep scrolling until the finger has lifted.

It seems like I would want to combine UISwipeGestureRecognizer with UILongPressGestureRecognizer to accomplish this. After a successful swipe (UISwipeGestureRecognizer), enable the UILongPressGestureRecognizer and start scrolling until a finger has lifted (UILongPressGestureRecognizer ends).

The problem is that UILongPressGestureRecognizer event does not occur after a successful swipe.

Heres an example:

// Create single swipe recognizer to recognize right swipes
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleSwipeFrom:)];
swipeRecognizer.delegate = self;
[_gestureView addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];

// Create long press recognizer to recognize right press and holds
longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeAndHoldFrom:)];
longPressRecognizer.delegate = self;
[longPressRecognizer setMinimumPressDuration:0.5];
[longPressRecognizer setEnabled:NO];
[_gestureView addGestureRecognizer:longPressRecognizer];
[longPressRecognizer release];

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

    if ([recognizer state] == UIGestureRecognizerStateEnded) {
        // Move one item in direction based on recognizer.direction
        [_longPressGestureRecognizer setEnabled:YES];
    }
}

If I keep my finger on the screen after the swipe, the UILongPressGestureRecognizer event is never called. Any ideas on how I can accomplish this?

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

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

发布评论

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

评论(1

李不 2024-11-07 08:09:51

尝试实现 UIGestureRecognizerDelegate 并使用此方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

这将允许两者同时运行。您需要将代码添加到手柄滑动并保持方法中,以确保它仅在滑动完成后运行。

干杯

Try implementing the UIGestureRecognizerDelegate and use this method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

This will allow the two to run simultaneously. You will need to add code into the handle swipe and hold method though in order to make sure it is only being run after the swipe is completed.

Cheers

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