Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the UIPinchGestureRecognizer scale value determined?

I was wondering if anyone knows how the UIPinchGestureRecognizer scale value is determined, or if there is a formula I could use to calculate a new scale value?

I have an app where I attach a UIPinchGestureRecognizer to an imageView, and in certain specific instances, I need to manually readjust a scale if it shrinks the imageView so it goes past a certain point of the screen. Thanks.

like image 543
Ser Pounce Avatar asked Nov 02 '11 06:11

Ser Pounce


People also ask

How does the uipangesturerecognizer work?

The UIPanGestureRecognizer calls this method when it first detects a pan gesture, then continuously as the user continues to pan and one last time when the pan completes — usually when the user’s finger lifts. The UIPanGestureRecognizer passes itself as an argument to this method.

What are uigesturerecognizer classes in iOS?

In iOS 3.0, Apple came to the rescue with UIGestureRecognizer classes. These provide a default implementation to detect common gestures like taps, pinches, rotations, swipes, pans and long presses. Using them not only saves a ton of code, but it also makes your apps work properly.

How to create a ticklegesturerecognizer in Swift?

Create a new file via File ▸ New ▸ File… and pick the iOS ▸ Source ▸ Swift File template. Name the file TickleGestureRecognizer. The constants that define what the gesture will need.


1 Answers

Given two initial points (touches), compute the distance between them using pythagorean theorem. Let this be distance be called the "initial distance".

For each successive update of the points, recompute the distance between the points and let this distance be called the "new distance".

scale = "new distance" / "initial distance". 

In case anyone doesn't actually know... pythagorean theorem is:

sqrtf(powf(b.x - a.x, 2.0f) + powf(b.y - a.y, 2.0f))

It's simple to understand the scaling formula... if your fingers are twice as far apart as they were when you started pinching, the zoom should be 2.0 (2x) - so plug in some numbers... 50px apart initially... 100px apart now = 100 / 50 = 2

like image 52
Steve Avatar answered Oct 17 '22 20:10

Steve