如何通过手势识别器检测圆形手势?

发布于 2024-10-10 08:56:47 字数 72 浏览 0 评论 0原文

我正在尝试实现一个类似音量控制的圆形按钮。目前我可以通过手势识别器检测滑动。如何检测按钮上的圆形手势(顺时​​针和逆时针)?谢谢!

I am trying to implement a volume-control-like round button. Currently I can detect swipes by the Gesture Recognizer. How can I detect a circular gesture (clockwise and anti-clockwise) on the button? Thanks!

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

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

发布评论

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

评论(4

最美的太阳 2024-10-17 08:56:47

您是否正在寻找一触式手势(如圆形滑块)或两触式手势(如抓住并转动现实世界的旋钮)?如果是后者,请查看 UIRotationGestureRecognizer

如果是前者,那你就只能靠自己了。您当然可以将您的解决方案实现为您自己的自定义手势识别器:这是Apple所期望的,并且有 一些帮助您入门的文档(尽管我还没有看到很多工作示例)。另请参阅如何正确子类化 UIGestureRecognizer

作为一般方法,我将手势区域视为圆环形状:具有中心 c、内半径 r1 和外半径 的区域r2。当用户着陆时,您可以使用毕达哥拉斯定理计算距 c 的距离,并使用您最喜欢的三角函数计算角度。这样,您就可以确定触摸是否在该区域内。当用户拖动时,您可以根据角度更新控制值。在某些时候,他们会触摸或拖动到区域之外,这将结束手势。我建议允许触摸在r1内部或r2之外偏离很远:手指是不精确的工具。

Are you looking for a one-touch gesture (like a circular slider), or a two-touch gesture (like grabbing and turning a real-world knob)? If the latter, take a look at UIRotationGestureRecognizer.

If the former, you're pretty much on your own. You can certainly implement your solution as your own custom gesture recognizer: this is expected by Apple, and there's some documentation to get you started (though I haven't seen many working examples out there). See also How to correctly subclass UIGestureRecognizer.

As a general approach, I'd think of the region for the gesture as a donut shape: a zone with center c, inner radius r1, and outer radius r2. When the user touches down you can calculate the distance from c using the Pythagorean theorem, and the angle with your favorite trig function. With that, you can determine whether the touch is within the zone. As the user drags, you can update the control value based on the angle. At some point, they'll either touch up, or drag outside of the zone, and that will end the gesture. I suggest allowing the touch to stray pretty far inside r1 or outside of r2: fingers are imprecise tools.

○闲身 2024-10-17 08:56:47

由于未能找到解决方案,并且通过 @sixten 答案的逻辑启发,我已经创建了一些有用的东西,它不是防弹的,但它似乎可以满足我现在需要的功能。

请任何人分叉它、克隆它、使用它、改进它,它是开源的,这是我的存储库链接:

https://bitbucket.org/ danielphillips/dpcirculargesturerecognizer

As failing to find a solution, and by logical inspiration from @sixten's answer, I've gone and created something useful, it's not bullet proof, but it seems to do what I need for now.

Please anyone, fork it, clone it, use it, improve it, it's open source, here's my repo link:

https://bitbucket.org/danielphillips/dpcirculargesturerecognizer

薆情海 2024-10-17 08:56:47

您是否尝试过编写自己的手势识别器?文档中有详细说明。

Have you tried writing your own gesture recognizer? There are detailed instructions in the docs.

冰火雁神 2024-10-17 08:56:47

最好的方法是实施自定义手势识别器。我不知道您是否希望检测谨慎手势(一旦检测到完整的圆就会调用事件处理方法)或继续手势(每次在圆形路径上移动手指时都会调用事件处理方法)。

也许,用户可能不会画一个完美的圆,@Sixten Otto 提供的解决方案会很好。

Best Way would be implement Custom Gesture Recognizer. I do not know if you wish to detect a Discreet Gesture (Event Handling method would be called once the full circle has been detected) or a Continues Gesture (Event Handling method would be called every time you move your finger on a circular Path).

Probably, the user may not draw a perfect circle, and the solution provided by @Sixten Otto would be good.

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