UIPinchGestureRecognizer如何正确使用它?

时间:2012-02-01 00:32:47

标签: ios

我有一个简单的图表控件,显示一些行。图表绘画涉及3个属性:

  • RecordCount
  • FirstVisibleIndex
  • LastVisibleIndex

一切正常,图表在First& amp;最后可见记录。 现在我想让用户放大&用两根手指拿出图表,所以我订阅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];
}

它有点工作,但非常不稳定,当我尝试移动手指并进行缩放操作时,图表会“跳跃”。 我不能模拟一个漂亮和平滑的放大&amp;经验。 是否有任何最佳方法可以实现VisibleIndexes的平滑变化,与图表中的记录数量和可见数量相关。

1 个答案:

答案 0 :(得分:1)

我要做的第一件事就是尝试绘制动画的性能图并找出导致问题的图层。尝试使用Core Animation工具在Instruments中的设备上运行。然后启用颜色混合图层,看看你看到多少红色。

查看我的Core Animation性能优化教程。 http://mobileoverlord.com/instruments-optimizing-core-animation/