Ios - Pinch / zoom from the current scale

The following code correctly clamps / scales the container view, but only after it moves to the 1.0 scale. How can I change it so that the view of the container scales from it at the current scale?

UIPinchGestureRecognizer *twoFingerPinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)]; [self.container addGestureRecognizer:twoFingerPinch]; - (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer { _scale = recognizer.scale; CGAffineTransform tr = CGAffineTransformScale(self.view.transform, _scale, _scale); self.container.transform = tr; } 
+6
source share
2 answers

In the .h file, add:

 CGFloat _lastScale; 

In the .m file,

 - (id)init { ... _lastScale = 1.0f; ... } - (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer { if (recognizer.state == UIGestureRecognizerStateEnded) { _lastScale = 1.0f; return; } CGFloat scale = 1.0f - (_lastScale - recognizer.scale); CGAffineTransform tr = CGAffineTransformScale(self.view.transform, scale, scale); self.container.transform = tr; _lastScale = recognizer.scale; } 
+9
source

Here is how I do it:

 - (void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer { static float initialDifference = 0.0; static float oldScale = 1.0; if (recognizer.state == UIGestureRecognizerStateBegan){ initialDifference = oldScale - recognizer.scale; } CGFloat scale = oldScale - (oldScale - recognizer.scale) + initialDifference; myView.transform = CGAffineTransformScale(self.view.transform, scale, scale); oldScale = scale; } 
+2
source

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


All Articles