如何通过手势识别器检测圆形手势? [英] How to detect circular gesture via Gesture Recognizer?

查看:153
本文介绍了如何通过手势识别器检测圆形手势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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!

推荐答案

您是在寻找单触手势(如圆形滑块)还是双触手势(喜欢抓住并转动现实世界的旋钮)?如果是后者,请查看 UIRotationGestureRecognizer

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.

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

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.

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

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.

这篇关于如何通过手势识别器检测圆形手势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆