UIscrollView 滑动手势 + iOS 上的 UIButton 手指按下效果

发布于 2024-12-28 14:30:01 字数 454 浏览 4 评论 0原文

我试图在分页模式下提出 UISCrollView,其中每个页面都显示产品的图像,除了能够在页面之间滑动之外,我还希望产品图像切换到“向下”版本当点击特定产品/页面时。

到目前为止,我尝试了以下操作:

1 - 添加 UIButtons 作为滚动视图的页面: 显然,这样我就可以在手指按下时将图像切换到“选定”版本,但是按钮,整个页面向上,防止滚动视图检测到滑动手势。

2 - 添加 UIVIews 而不是按钮,以及 UITapGestureRecognizer: 这样我就可以点击图像来选择产品,并且识别器还可以让手势传递到滚动视图,也允许滑动。但这种方法的问题是,当用户触摸图像时,我无法将图像切换到“选定”版本,因为点击识别器仅报告 UIGestureRecognizerStateEnded。

关于如何同时获得按钮向上/向下和滚动视图滑动行为的任何想法?

I'm trying to come up with a UISCrollView in paging mode, where each page shows an image of a product and, besides being able to swipe between pages, I'd also like for the product image to switch to a "down" version of it when a specific product/page is tapped.

So far I tried the following:

1 - adding UIButtons as the pages of the scrollview: obviously, this way I can have the images switch to their "Selected" version on finger down, but the buttons, taking up the whole page, prevent the scrollview from detecting the swipe gesture.

2 - adding UIVIews instead of buttons, and an UITapGestureRecognizer: this way I can tap an image to select a product and the recognizer also lets the gesture pass on to the scrollview, allowing for swiping too. But the problem with this approach is that I can't switch images to their "selected" versions when the user touches them, since the tap recognizer only reports UIGestureRecognizerStateEnded.

Any ideas as to how to get both the button up/down and scrollview swipe behaviors?

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

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

发布评论

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

评论(1

无需解释 2025-01-04 14:30:01

废弃一切(当然不是一切,但你明白了要点)。没有手势识别器就什么都没有。我最近也遇到了这个问题,在 UIScrollview 上安装了一个 UIView,需要在其中包含一些交互元素。解决方案是 UIView 的一个非常棒的属性,叫做 ExclusiveTouch。独占触摸从滚动视图中获取所有事件,如果事件位于 UIView 内部,则忽略它们,然后将它们直接传递到您的视图。而且因为 UIButton 继承自 UIView,所以您所需要的只是 self.button.exclusiveTouch = YES

很酷,对吧!?

Scrap everything (well not everything, but you get the gist). No gesture recognizers no nothing. I recently had this problem too, having mounted a UIView on a UIScrollview that needed to have some interactive elements in it. The solution, UIView's friggin awesome property called exclusiveTouch. Exclusive touch takes all of the events from the scrollview, and ignores them if the event is inside the UIView, then passes them directly to your view. And because UIButton inherits from UIView, all you need is self.button.exclusiveTouch = YES

Pretty cool, huh!?

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