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.