How is the value of the UIPinchGestureRecognizer scale determined?

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

I have an application where I attach a UIPinchGestureRecognizer to an imageView , and in some specific cases I need to manually adjust the scale if it compresses the imageView , so it passes a certain point on the screen, Thanks.

+4
source share
2 answers

I found out what I need by doing a little reverse engineering. Since most of you know who implemented the scaling method using UIPinchGestureRecognizer, you will end up with this line of code:

 GFloat nextScale = 1 - (previousScale - [sender scale]); 

This is the next scale, calculated on the value set by the UIPinchGestureRecognizer, however I had to create my own custom scale as the user went beyond the limits I set. So I determined the width at which I needed my image to be compressed, and determined the adjusted scale that I needed by setting it as the proportion: (nextScale / adjustScale) = (nextWidth / adjustWidth).

Then I again entered the modified scale in the above formula to determine what [sender's scale] will be for my new scale amount: [sender's scale] = AdjustedScale - 1 - previousScale.

So now I can use this amount to set the value of previousScale, which I will need next time.

+1
source

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

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

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

If someone doesn’t really know ... Pythagoras theorem:

sqrtf(powf(bx - ax, 2.0f) + powf(by - ay, 2.0f))

It’s easier to understand the scaling formula ... if your fingers are two times apart from each other, as it was when you started pinching, the scale should be 2.0 (2x) - so connect some numbers ... 50 pixels first .. .100px split now = 100/50 = 2

+5
source

Source: https://habr.com/ru/post/1379255/


All Articles