0

I have created a graph with Core Plot and am trying to use the pinch zoom feature. I want the pinch to work in just the x axis or just the y axis. I have accomplished this by using the following code in a pinch gesture recognizer that I put in.

- (void)handlePinch:(UIPinchGestureRecognizer*)recognizer
{
//NSLog(@"We are pinching");
//This allows you to figure out which direction the pinch is occuring HORIZ or VERT

if (recognizer.state != UIGestureRecognizerStateCancelled)  //If gesture is occuring
{
    _startScale = 1.0f;

    if (recognizer.numberOfTouches == 2)  //With two fingers
    {
        CGPoint firstPoint = [recognizer locationOfTouch:0 inView:recognizer.view];
        CGPoint secondPoint = [recognizer locationOfTouch:1 inView:recognizer.view];

        CGFloat tangent = fabs(secondPoint.y - firstPoint.y) / (secondPoint.x - firstPoint.x);

        CGFloat angle = fabs(tangent);
        //NSLog(@"angle is %f", angle);

        if (angle <= 0.2679491924f)
        {
            //NSLog(@"HORIZONTAL");
            _zoomX = _startScale * recognizer.scale;

        }
        else if (angle >= 3.7320508076f)
        {
            //NSLog(@"VERTICAL");
            _zoomY = startScale * recognizer.scale;
        }
        else
        {
            //NSLog(@"BOTH");
        }
    }
}

[self changePlotRange];

}

The method changePlotRange is as follows.

- (void)changePlotRange
{

CPTGraph *graph = _hostView.hostedGraph;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;

plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromCGFloat(_Xmin * _zoomX) length:CPTDecimalFromCGFloat((_Xmax - _Xmin) * _zoomX)];

plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromCGFloat(_Ymin * _zoomY) length:CPTDecimalFromCGFloat((_Ymax - _Ymin) * _zoomY)];
}

This works great and I can zoom in and out on either axis independently. However, when I start again to pinch, the scale jumps back to one and the graph bounces back to the original scale so you need to scale again. I want this to work like a picture, pinch a little, zoom a little, pinch some more and the picture zooms from the last scale.

So within the pinch gesture, I tried to keep the current scale and return it later with...

if (recognizer.state == UIGestureRecognizerStateEnded)
{
    _startScale = _zoomX;
}

I put this inside the handlePinch method. But it still goes back to a scale of one. Any help would be appreciated as I have searched and tried numerous things over the past three days.

Douglas
  • 2,524
  • 3
  • 29
  • 44

1 Answers1

1

You need to save the ending zoom values for both x and y separately since they will probably be different. Also, don't reset the start scale at the beginning of the gesture recognizer handler—you want it to use the previous zoom value.

Eric Skroch
  • 27,381
  • 3
  • 29
  • 36
  • Thank you Eric, I have it almost figured out! I am able to keep the zoom scale so it works the way I want, but sometimes the zoom becomes so large I lose my data points. – Douglas Dec 07 '15 at 15:14
  • You can set the `globalXRange` and `globalYRange` to limit the maximum extent of the plot space. – Eric Skroch Dec 07 '15 at 15:24
  • That's too funny, I just implemented that! Thanks, it works, I will accept the answer. Another question, if you look at my code, when I pinch inward, the scale gets bigger, when I pinch outward, the scale gets smaller, any idea how to switch those up? Thanks again for all your help. – Douglas Dec 07 '15 at 16:16