ios - UIPinchGestureRecognizer 如何正确使用?

标签 ios

我有一个显示一些线条的简单图表控件。图表绘制涉及3个属性:

  • 记录数
  • 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 相对于我在图表中有多少记录以及有多少可见的平滑变化。

最佳答案

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

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

关于ios - UIPinchGestureRecognizer 如何正确使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9089148/

相关文章:

ios - 如何在特定索引处将数组插入现有NSMutableArray中

ios - 如何在 iOS 中创建空白 CSV 文件

ios - 无法快速访问 JSON 数组

iOS 8 : UITableViewCell. contentView 不调整其中的 subview

ios - 如何在 Swift 中将核心数据查询集转换为可变数组?

ios - 解析查询一开始无法正常工作 iOS Swift

ios - 创建一个 View ,如 iOS 的图片所示

ios - dequeueReusableCellWithIdentifier 索引越界

ios - 如何让按钮从下午 3 点到晚上 9 点打开?

ios - 归档 : 'GoogleAnalytics/GAI.h' file not found 时出错