How to do pinch gestures on the iPhone?

Here's a tutorial and another question with an answer that links to some great code.

Up vote 9 down vote favorite 7 share g+ share fb share tw.

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. Iphone multi-touch gesture-recognition pinch link|improve this question edited Jul 10 '09 at 3:58 asked Jul 10 '09 at 3:43RexOnRoids2,59573986 97% accept rate.

1 look this sample on pinch zoom cocoabugs.blogspot.com/2011/03/… – jeeva Mar 10 '11 at 2:47.

1 That's a hands down bad tutorial... – Jonny Oct 27 '10 at 6:43.

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.

Ummm...Not sure about how much that 'dot product' link you cited will come in handy but thanks for a response at least... – RexOnRoids Jul 10 '09 at 4:01 Without it you can't calculate the distance between the two touches. It is essential. – Brock Woolf Jul 10 '09 at 4:07 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 Jul 10 '09 at 4:10 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 Jul 10 '09 at 4:14 I'll explain it in my answer.....give me a sec – Brock Woolf Jul 10 '09 at 4:22.

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

2 It's not so relevant as long as there are tons of.

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!

Dot can be used to find distance between two points by the following: sqrt(dot(p2-p1,p2-p1)) – twerdster Oct 24 '11 at 13:25.

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).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions