views:

4982

answers:

5

How does one implement pinch gestures on the iPhone? I'm familiar with using touch events on the iPhone but I'm extremely lazy and don't want to re-invent the wheel for something as widespread as PINCH gestures...Source code or links thereto would be helpful.

+4  A: 

Here's a tutorial.

marcc
That's a hands down bad tutorial...
Jonny
+6  A: 

You need to implement it yourself using some basic math.

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

Those are the delegate methods that you need to implement in your application.

You need to get the distance between the two touches, then calculate the changes in distance in your own code.

The mathematical equation you use to get the distance is called the dot product

I just want to point out that the equation they use in the cited tutorial is incorrect. I have updated it to include the absolute value that is missed in the tutorial.

This is the dot product:

- (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint
{
    float xDist = fromPoint.x - toPoint.x;
    float yDist = fromPoint.y - toPoint.y;

    float result = sqrt( pow(xDist,2) + pow(yDist,2) );
    return result;
}

Edit: I made a mistake in my previous answer....it's been a while since I've done any of that kind of math.

If you don't square root the result, you won't get the true distance between the points. This is called calculating the magnitude between two vectors.

This is the proper way to do it. If you want to omit the sqrt, you will not have the exact distance between the two points.

If you don't square the answer, your code will work but it will be using measurements that are much larger than the actual values. So in the future if you need to get the distance between the two points, it will return something like 90000 instead of the actual pixel distance which would be 300px.

Brock Woolf
Ummm...Not sure about how much that 'dot product' link you cited will come in handy but thanks for a response at least...
RexOnRoids
Without it you can't calculate the distance between the two touches. It is essential.
Brock Woolf
Thx -- Source code is much easier to understand in this case.. I wasn't prepared to sit down and attempt to absorb an entire wiki page on the intricacies of the 'dot product' LOL
RexOnRoids
Then again, why do I need to fiddle with square roots (in your provided method) just to get the distance between two points? I usually just do something like: differenceX = pointA.x - pointB.x; differenceY = pointA.y - pointB.y;...So, what am I missing?
RexOnRoids
I'll explain it in my answer.....give me a sec
Brock Woolf
Thx for the clarification. BTW, Have you ever implemented a pinch gesture function in your experience as a Games programmer? If so, could you pass along some source to help us along? I'm sure many people will benefit. Thx.
RexOnRoids
If you could give some source relevant to pinch gesture implementation I'll give you the check-mark. Otherwise it goes to marcc who provided a link to a tutorial that seems more relevant.
RexOnRoids
Doesn't `sqrt` always return positive? Why take the absolute value?
Ben Alpert
+1  A: 

If you're looking for a simple code example of the pinch gesture, I use the method described by Brock in the example application I describe here (in the second-to-last and last versions of the application on that page). The pinch gestures are used to scale a couple of Core Animation layers in that example.

All he's describing as far as the distance calculation is simply the Pythagorean Theorem applied between two points (with the X and Y distances as the two faces of a right triangle).

Brad Larson
+2  A: 

hmmm.....it seems that some people are extremely confused on how to use basic math. the dot product is totally irrelavent and just confuses the issues. dot prod between 2 vectors is used to calculate the angle between them.

You actually need to use pythagorus theorem. Dot is NOTHING to do with finding distance between points - only pythagorous!

Nige
+1  A: 

Pretty sure the solutions presented here have been obsoleted, in favor of using the new UIPinchGestureRecognizer class in iOS 3.2 and later.

Greg Maletic
It's not so relevant as long as there are tons of <3.2 devices out there.
Jonny