Andy Jacobs
Andy Jacobs

Reputation: 15245

Detecting Special touch on the iphone

I was asking myself if there are examples online which covers how you can for instance detect shapes in touch gestures.

for example a rectangle or a circle (or more complex a heart .. )

or determine the speed of swiping (over time ( like i'm swiping my iphone against 50mph ))

Upvotes: 3

Views: 2788

Answers (2)

Jarson
Jarson

Reputation: 121

There does exist other methods for detecting non-simple touches on a touchscreen. Check out the $1 unistroke gesture recognizer at the University of Washington. http://depts.washington.edu/aimgroup/proj/dollar/

It basically works like this:

  1. Resample the recorded path into a fixed number of points that are evenly spaced along the path
  2. Rotating the path so that the first point is directly to the right of the path’s center of mass
  3. Scaling the path (non-uniformly) to a fixed height and width
  4. For each reference path, calculating the average distance for the corresponding points in the input path. The path with the lowest average point distance is the match.

What’s great is that the output of steps 1-3 is a reference path that can be added to the array of known gestures. This makes it extremely easy to give your application gesture support and create your own set of custom gestures, as you see fit.

This has been ported to iOS by Adam Preble, repo on github: http://github.com/preble/GLGestureRecognizer

Upvotes: 0

unwesen
unwesen

Reputation:

For very simple gestures (horizontal vs. vertical swipe), calculate the difference in x and y between two touches.

dy = abs(y2 - y1)
dx = abs(x2 - x1)
f = dy/dx

An f close to zero is a horizontal swipe. An f close to 1 is a diagonal swipe. And a very large f is a vertical swipe (keep in mind that dx could be zero, so the above won't yield valid results for all x and y).

If you're interested in speed, pythagoras can help. The length of the distance travelled between two touches is:

l = sqrt(dx*dx + dy*dy)

If the touches happened at times t1 and t2, the speed is:

tdiff = abs(t2 - t1)
s = l/tdiff

It's up to you to determine which value of s you interpret as fast or slow.

You can extend this approach for more complex figures, e.g. your square shape could be a horizontal/vertical/horizontal/vertical swipe with start/end points where the previous swipe stopped.

For more complex figures, it's probably better to work with an idealized shape. One could consider a polygon shape as the ideal, and check if a range of touches

  1. don't have too high a distance to their closest point on the pologyon's outline, and
  2. all touches follow the same direction along the polygon's outline.

You can refine things further from there.

Upvotes: 8

Related Questions