使用 UIGestureRecognizer 中断滚动/动画 UIScrollView

发布于 2024-11-03 10:35:44 字数 1191 浏览 0 评论 0 原文

我有一个 UIScrollView 和一个 UITapGestureRecognizer,它们都非常基本,并且让它们以我需要的各种方式协同工作,除了其中一种。 UIScrollView 不使用任何缩放,而 UITapGestureRecognizer 目前仅写入控制台。

当点击 UIScrollView 时,无论是在静止状态还是通过 setContentOffset 进行动画处理,我都可以让 UITapGestureRecognizer 写入控制台,但我无法获取它当 UIScrollView 由于滑动而移动时工作。当 UIScrollView 被滑动并仍在运动时,滑动后的第一次点击将使其停止移动,然后只有第二次点击才会被 UITapGestureRecognizer 拾取。我希望第一次点击即可停止 UIScrollView 滚动,并通过 UITapGestureRecognizer 写入控制台。

我希望我的问题只是由于我对 UIScrollViewUITapGestureRecognizer 的了解存在差距,并且只有一个属性可以设置来解决此问题,但到目前为止还没有大量的阅读帮助我解决了这个问题。关于这是否可能有什么想法吗?

编辑:感谢您的建议,请参阅下文

抱歉,我认为我没有很好地解释自己。我意识到固定 UIScrollView 上的第一个动作是滑动(在我的例子中,它只是由 UIScrollView 顺便说一句,而不是手势识别器处理)。

问题是在滑动并释放后,如果您在其仍在运动时点击(并且没有其他触摸正在进行中),则 UITapGestureRecognizer 不会拾取它,相反,它仅由 UIScrollView 拾取并停止 UIScrollView 移动。如果你让它减速然后点击,效果很好。此外,如果它是由于动画而不是滑动而移动,则点击也可以正常工作。

这与使用“UILongPressGestureRecognizer”时的情况相同,这是我理想中想要使用的,但我想如果我不能让它通过点击来工作,我就没有机会了!

I have a UIScrollView and a UITapGestureRecognizerwhich are both pretty basic, and have got them to work together in every way I need except one. The UIScrollView doesn't use any zooming, and the UITapGestureRecognizer just writes to the console for now.

I can get the UITapGestureRecognizer to write to the console when the UIScrollView is tapped, either while stationary or animating through setContentOffset, but I cannot get it to work when the UIScrollView is moving due to being swiped. When the UIScrollView is swiped and still in motion, the first tap after swiping will stop it moving, and then only the second tap is picked up by the UITapGestureRecognizer. I hope to get the first tap to both stop the UIScrollView from scrolling and also write to the console through the UITapGestureRecognizer.

I hope that my problem here is just through a gap in my knowledge of either the UIScrollView or UITapGestureRecognizer and there is just a Property to set to fix this, but so far no amount of reading has helped me with this issue. Any ideas on whether this is possible?

Edit: Thanks for the suggestions, please see below

Apologies, I don't think I explained myself very well. I realise the first movement on the stationary UIScrollView is a swipe (which in my case is just handled by the UIScrollView btw, not a gesture recognizer).

The problem is after swiping and releasing, if you tap while it is still in motion (and no other touch is in progress), it isn't picked up by the UITapGestureRecognizer, instead it is only picked up by the UIScrollView and stops the UIScrollView moving. If you let it decelerate then tap, this works fine. Also if it is moving due to an animation but not a swipe, the tap also works fine.

This is the same when using a `UILongPressGestureRecognizer' which is what I want to use ideally, but thought if I can't get it to work with a tap, I have no chance with that!

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

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

发布评论

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

评论(2

枉心 2024-11-10 10:35:44

听起来您需要实现

<代码>- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

为您的 UITapGestureRecognizer 委托方法,并为 YES 返回 <代码>UIGestureRecognizers您想要同步解释点击手势。

ex

// Somewhere in your code...
UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
tap.delegate = self;
[self.view addGestureRecognizer:tap];
[tap release];

// And the delegate method...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
        return YES;
}

nb 您可能希望在决定与哪个 UIGestureRecognizer 同步时更加具有辨别力。该示例适用于所有其他手势识别器。

Sounds like you need to implement the

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

delegate method for your UITapGestureRecognizer and return YES for the UIGestureRecognizers you want to interpret tap gestures in sync with.

e.x.

// Somewhere in your code...
UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
tap.delegate = self;
[self.view addGestureRecognizer:tap];
[tap release];

// And the delegate method...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
        return YES;
}

n.b. You may want to be more discriminating in deciding which UIGestureRecognizer(s) you wish to work in sync with. The example will work with every other gesture recognizer.

风为裳 2024-11-10 10:35:44

问题是,当用户第一次点击滚动UIScrollView时,这不是点击,而是滑动。仅当用户在几乎相同的位置点击并释放手指(而不在屏幕上拖动手指)时,才会被视为点击。

您可以采取的措施来解决您的问题是:使用 UIScrollView 委托方法 scrollViewWillBeginDragging 然后您就会知道用户何时刚刚点击并开始拖动滚动视图,并且还添加用于这些真实点击的 UITapGestureRecognizer

The problem is that when the user taps the first time to scroll the UIScrollView, this is not a tap, it is a swipe. It would be considered a tap only when the user taps and releases the finger almost at the same position (without dragging the finger through the screen).

What you could do to solve your problem is: use the UIScrollView delegate method scrollViewWillBeginDragging and then you would know when the user just tapped and will start dragging the scrollview, and also add the UITapGestureRecognizer for those real taps.

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