cannyboy
cannyboy

Reputation: 24426

How to detect a circle motion with UIGestureRecognizer

I want to be able to detect someone's finger drawing a circular motion on the screen - as if they were drawing an 'O'. Is this possible with UIGestureRecognizer?

Upvotes: 4

Views: 4911

Answers (6)

Jim True
Jim True

Reputation: 1080

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

Upvotes: 0

cannyboy
cannyboy

Reputation: 24426

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.

Upvotes: 1

martin's
martin's

Reputation: 3955

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.

Upvotes: 4

Jason Cragun
Jason Cragun

Reputation: 3233

I think this may be what you need

How to detect circular gesture via Gesture Recognizer?

Upvotes: 2

Jano
Jano

Reputation: 63707

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

Upvotes: 1

albianto
albianto

Reputation: 4152

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

Upvotes: 0

Related Questions