How to detect circular gesture via Gesture Recognizer?

前端 未结 4 2077
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 12:25

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

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 12:49

    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.

提交回复
热议问题