2

I have a simple chart control that display some lines. There are 3 properties involved in chart painting:

  • RecordCount
  • FirstVisibleIndex
  • LastVisibleIndex

Everything works ok, chart paints just fine between First & Last Visible Record. Now I want to let user zoom in & out the chart using 2 fingers, so I subscribed to UIPinchGestureRecognizer and implemented such a routine.

-(void)handleZoomGesture:(UIGestureRecognizer *)sender
{
    if (_isChartBusy) {
        return;
    } 

    UIPinchGestureRecognizer *gr = (UIPinchGestureRecognizer*)sender;
    if (gr.state != UIGestureRecognizerStateChanged && gr.state != UIGestureRecognizerStateEnded)
        return;

    CGFloat scale = gr.scale;
    if (scale == 1) {
        return  ;
    }

    if (_prevZoomScale == 0) {
        _prevZoomScale = scale;
        return;
    }

    int startIndex = chart.FirstVisibleIndex;
    int endIndex = chart.LastVisibleIndex;

    NSLog(@"Zoom. Scale: %f", scale);
    int stepZoom;
    int cnt = chart.LastVisibleIndex - chart.FirstVisibleIndex;
    stepZoom = cnt * 0.05;


    if (scale < _prevZoomScale) {
        startIndex -= stepZoom;
        endIndex += stepZoom;
    } else {
        startIndex += stepZoom;
        endIndex -= stepZoom;
    }

    _prevZoomScale = scale;

    if (endIndex < startIndex || (endIndex - startIndex) < 10) {
        return;
    }

    if (startIndex < 0) {
        startIndex = 0;
    }

    if (endIndex >= [chart.DataProvider GetBarRecordCount]) {
        endIndex = [chart.DataProvider GetBarRecordCount] - 1;
    }

    if (startIndex == chart.FirstVisibleIndex && endIndex == chart.LastVisibleIndex) {
        return;
    }

    chart.FirstVisibleIndex = startIndex;
    chart.LastVisibleIndex = endIndex;
    [self setNeedsDisplay];
}

it kinda works, but very unstable, chart gets "jumpy" when I try to move fingers and make the zoom operation. I cannot simulate a nice and smooth zoom in & out experience. Are there any best approaches to achieve such a smooth change of VisibleIndexes in relation to how many Records I have in chart and how many are visible.

millimoose
  • 39,073
  • 9
  • 82
  • 134
Eugen
  • 2,934
  • 2
  • 26
  • 47

1 Answers1

1

The first thing I would do is try to graph the animation for performance and find out which layers are causing problems. Try running on a device in Instruments with the Core Animation tool. Then enable color blended layers and see how much red you see.

Check out my tutorial on Core Animation performance optimization. http://mobileoverlord.com/instruments-optimizing-core-animation/

MobileOverlord
  • 4,580
  • 3
  • 22
  • 30
  • Question is not about performance, chart paints pretty fast. My problem is how to operate correctly with chart properties that I have within gesture callback to achieve a smooth zoom in & out experience. – Eugen Feb 04 '12 at 04:55
  • What are you using to chart? Are you drawing directly with Core Graphics – MobileOverlord Feb 04 '12 at 05:49
  • yes, I do draw using Quartz, but that's not about painting. Is just about how to calculate correct the properties (FirstVisibleIndex, LastVisibleIndex) using values that I get in gesture callback. – Eugen Feb 05 '12 at 03:11