2
我有一個簡單的圖表控件,顯示一些行。有3個屬性涉及圖繪畫:UIPinchGestureRecognizer如何正確使用它?
RecordCount
FirstVisibleIndex
LastVisibleIndex
一切工作正常,圖表油漆首先&最後一個可見的記錄之間就好了。 現在我想讓用戶使用2個手指放大圖表&,所以我訂閱了UIPinchGestureRecognizer
並實現了這樣一個例程。
-(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];
}
它有點兒工作,但很不穩定,圖表變得「神經質」當我嘗試移動手指,使變焦操作。 我無法模擬一個不錯的平滑放大&的體驗。 是否有任何最佳方法可以實現VisibleIndexes的平滑變化,這與我在圖表中記錄多少個記錄以及多少個可見。
問題不在於表現,圖表塗料相當快。我的問題是如何正確使用我在手勢回調中實現平滑放大和縮小體驗的圖表屬性。 – Eugen 2012-02-04 04:55:45
你在用什麼圖表?你是直接使用Core Graphics – MobileOverlord 2012-02-04 05:49:22
是的,我使用Quartz繪製,但這不是關於繪畫。只是關於如何使用手勢回調中獲得的值來計算正確的屬性(FirstVisibleIndex,LastVisibleIndex)。 – Eugen 2012-02-05 03:11:27