开发者

How to detect a circle motion with UIGestureRecognizer

开发者 https://www.devze.com 2023-03-01 13:44 出处:网络
I want to be able to detect someone\'s finger drawing a 开发者_如何学Gocircular motion on the screen - as if they were drawing an \'O\'. Is this possible with UIGestureRecognizer?I think the answer to

I want to be able to detect someone's finger drawing a 开发者_如何学Gocircular motion on the screen - as if they were drawing an 'O'. Is this possible with UIGestureRecognizer?


I think the answer to this depends on your definition of circular motion and how you intend to use it. For example, do you want to know how many degrees along a circle the users finger has travelled? Or, do you only care about a circle being completed? What is the degree of accuracy you require? Do you want to allow for the motion to be interrupted or does this have to be more of a touch-down > draw-circle > touch-up (in other words, single motion)?

One approach would be to define a bunch of rectangular zones along the circumference and detect if the user is touching these in sequence. This can provide you with direction and a coarse indication of angle.

Another approach is to store the points between touch down and touch up and do some filtering and curve fitting to figure out what shape is approximated by the points. First low-pass-filter using a basic FIR filter and then look at the dx and dy from point to point. A circle (as a series of arcs) will have to fall within a certain range of slope changes from point to point, otherwise you have some other shape.

Yet another approach is to use a Neural Network to take the points and tell you what the shape looks like.


I think this may be what you need

How to detect circular gesture via Gesture Recognizer?


Instead using a gesture recognizer, this project reacts to circular motions tracking the angle of UITouch events.


My answer to my question:

I used this: http://iphonedevelopment.blogspot.com/2009/04/detecting-circle-gesture.html

.. but turned the CircleView into a custom UIGestureRecognizer. Everything lovely.


No, it doesn't recognize natively a circular motion. You have to implement your own method to do that.


Here's how i needed to do it using the touches callbacks in my view controller but this could be made into a gesture too. Note, I was trying to detect multiple circle motions (2 or more clockwise or counterclockwise circles made during a touch event.

  1. Store touchesMoved CGPoints in an array.
  2. Create a min/max rect of all the points in your history array.
  3. Divide this min/max rect into 4 smaller rects.
  4. Assign each history point a quadrant using CGRectContainsPoint() for each of the 4 quadrants
  5. A clockwise motion will have quadrants ascending. A counter-clockwise motion will have quadrants descending.
  6. Check the ratio of width/height if you want to detect circles vs ovals
0

精彩评论

暂无评论...
验证码 换一张
取 消