Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios - Pinch/zoom from current scale

The following code correctly pinches/zooms the container view, but only after it jumps to a scale of 1.0. How can I modify it so that the container view scales from it's 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;
}
like image 409
soleil Avatar asked May 28 '12 06:05

soleil


2 Answers

In .h file, add:

CGFloat _lastScale;

In .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;
}
like image 198
Hanon Avatar answered Oct 18 '22 02:10

Hanon


Here's 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;    

}
like image 26
Phil Turner Avatar answered Oct 18 '22 04:10

Phil Turner