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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With