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

时间:2022-09-06 20:59:46

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!

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

4 个解决方案

#1


15  

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.

您是在寻找单触手势(如圆形滑块)还是双触手势(如抓住并转动真实世界的旋钮)?如果是后者,请查看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.

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

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

#2


6  

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.

由于没有找到解决方案,并且从@ sixten的回答中获得了逻辑灵感,我已经去创造了一些有用的东西,它不是防弹,但它似乎做了我现在需要的东西。

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

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

https://bitbucket.org/danielphillips/dpcirculargesturerecognizer

https://bitbucket.org/danielphillips/dpcirculargesturerecognizer

#3


0  

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

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

#4


0  

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).

Best Way将实现Custom Gesture Recognizer。我不知道您是否希望检测到Discreet Gesture(一旦检测到整圆就会调用事件处理方法)或Continues Gesture(每次在圆形路径上移动手指时都会调用事件处理方法)。

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

可能用户可能没有画出完美的圆圈,而@Sixten Otto提供的解决方案也不错。

#1


15  

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.

您是在寻找单触手势(如圆形滑块)还是双触手势(如抓住并转动真实世界的旋钮)?如果是后者,请查看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.

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

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

#2


6  

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.

由于没有找到解决方案,并且从@ sixten的回答中获得了逻辑灵感,我已经去创造了一些有用的东西,它不是防弹,但它似乎做了我现在需要的东西。

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

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

https://bitbucket.org/danielphillips/dpcirculargesturerecognizer

https://bitbucket.org/danielphillips/dpcirculargesturerecognizer

#3


0  

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

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

#4


0  

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).

Best Way将实现Custom Gesture Recognizer。我不知道您是否希望检测到Discreet Gesture(一旦检测到整圆就会调用事件处理方法)或Continues Gesture(每次在圆形路径上移动手指时都会调用事件处理方法)。

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

可能用户可能没有画出完美的圆圈,而@Sixten Otto提供的解决方案也不错。