0

我尝试将核心图缩放限制在 2 值之间,或多或少我的代码工作,但有时它不能正常工作,这是代码:

- (void) handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer{

        if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
            scale = [gestureRecognizer scale];

        }
        else if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {

            CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;

            lenghtX = [self getRoundedValue:max_X - min_X];
            lenghtY = [self getRoundedValue:max_Y - min_Y];


            if (lenghtX <= 100 && lenghtX >= 2) {

                double stepScale = lenghtX / 10;

                if ([gestureRecognizer scale] < scale) {

                    min_X = [self getRoundedValue:plotSpace.xRange.minLimitDouble - stepScale];
                    max_X = [self getRoundedValue:plotSpace.xRange.maxLimitDouble + stepScale];
                    min_Y = [self getRoundedValue:plotSpace.yRange.minLimitDouble - stepScale];
                    max_Y = [self getRoundedValue:plotSpace.yRange.maxLimitDouble + stepScale];
                }
                else {

                    min_X = [self getRoundedValue:plotSpace.xRange.minLimitDouble + stepScale];
                    max_X = [self getRoundedValue:plotSpace.xRange.maxLimitDouble - stepScale];
                    min_Y = [self getRoundedValue:plotSpace.yRange.minLimitDouble + stepScale];
                    max_Y = [self getRoundedValue:plotSpace.yRange.maxLimitDouble - stepScale];
                }
            }
            else {

                if (lenghtX > 100) {

                    double rest = [self getRoundedValue:(lenghtX - 100)/2];

                    min_X = [self getRoundedValue:min_X + rest];
                    max_X = [self getRoundedValue:max_X - rest];
                    min_Y = [self getRoundedValue:min_Y + rest];
                    max_Y = [self getRoundedValue:max_Y - rest];

                    NSLog(@"Lenght:%f Rest: %f",lenghtX,rest);

                }
                else if (lenghtX < 2) {

                    double rest = [self getRoundedValue:(2 - lenghtX)/2];

                    min_X = [self getRoundedValue:min_X - rest];
                    max_X = [self getRoundedValue:max_X + rest];
                    min_Y = [self getRoundedValue:min_Y - rest];
                    max_Y = [self getRoundedValue:max_Y + rest];

                    NSLog(@"Lenght:%f Rest: %f",lenghtX,rest);

                }
            }

            lenghtX = [self getRoundedValue:max_X - min_X];
            lenghtY = [self getRoundedValue:max_Y - min_Y];

             NSLog(@"Lenght: %f",lenghtX);

            plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(min_X) length:CPTDecimalFromFloat(lenghtX)];
            plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(min_Y) length:CPTDecimalFromFloat(lenghtY)];

            scale = [gestureRecognizer scale];
        }
        else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {

            if (lenghtX <= 100 && lenghtX >= 2) {

                CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;

                min_X = [self getRoundedValue:plotSpace.xRange.minLimitDouble];
                max_X = [self getRoundedValue:plotSpace.xRange.maxLimitDouble];

                step_punto = (max_X-min_X)/numero_punti;

                [self get_N_Value:numero_punti  from:min_X toMax:max_X];
            }
        }
    }

- (double) getRoundedValue:(double) value {
    double roundedValue = round(value * 100.0) / 100.0;
    return roundedValue;
}

在这段代码中,我检查可见 x 的长度,如果 >100 或 <2,我将其设置为 2 或 100,问题是最终值并不总是在这个 renege 内。第二个问题是,当我放大到极限时(例如,当我放大时)我想阻止缩放,而现在在极限附近有一点缩小,然后一点点放大(重复)。

如何改进代码?

4

2 回答 2

0

您应该在这里检查接受的答案: Core Plot - set zoom level to show only part of graph

这是使用全局 X 和 Y 范围设置。这对我来说非常有效。

评论中还提到了使用 CPTPlotSpaceDelegate 的方法:

-(BOOL)plotSpace:(CPTPlotSpace *)space shouldScaleBy:(CGFloat)interactionScale aboutPoint:(CGPoint)interactionPoint;

这将允许您保持最大缩放级别。请注意,最小缩放级别实际上是通过设置全局 X 和 Y 范围来处理的。

于 2014-12-15T16:42:59.570 回答
0

删除以 开头的外部 if-else 语句if (lenghtX <= 100 && lenghtX >= 2)。始终计算缩放范围,然后检查长度并根据需要进行调整,使其回到 2-100 范围内。

于 2013-10-27T21:44:59.943 回答