2

我正在使用下面的代码沿 X 轴 Y 轴滚动我的核心图并进行缩放。它工作正常。但是当我放大我的核心情节时,它会在两个方向上放大。如果我沿 x 方向捏合,我希望绘图沿 X 缩放,如果沿 Y 方向捏合,则缩放 Y。有人可以帮我解决这个问题吗?

-(CGPoint)plotSpace:(CPTPlotSpace *)space willDisplaceBy:(CGPoint)displacement
{
    return CGPointMake(displacement.x, displacement.y);
}

-(CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate
{                                                                                                                                                                                                                                           
    // Adjust axis to keep them in view at the left and bottom;
    // adjust scale-labels to match the scroll.

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.hostView.hostedGraph.axisSet;
    if (coordinate == CPTCoordinateX) {
        axisSet.yAxis.orthogonalCoordinateDecimal = newRange.location;
    }

    else {
        axisSet.yAxis.titleLocation = CPTDecimalFromFloat(newRange.locationDouble +                                                  (newRange.lengthDouble / 2.0F));
    }

    return newRange;
}
4

2 回答 2

1

将坐标轴和标题保持在正确位置的最简单方法是使用axisConstraints坐标轴并将其保留为titleLocation默认的 NAN。这将减轻您的代表更新这些项目的责任,您可以专注于缩放。

在这两个委托方法中,您只需要-plotSpace:willChangePlotRangeTo:forCoordinate:. 另一个仅在滚动时调用。

决定是否要允许在 x 或 y 中进行缩放(请参阅原始问题评论中的链接)。检查coordinate委托方法中的参数;返回newRange以允许缩放或[space plotRangeForCoordinate:coordinate]恢复原始范围并防止缩放。

如果您需要使用自己的手势识别器来检测捏合角度,allowPinchScaling请在托管视图上设置为 NO 以禁用内置识别器。将您自己的识别器添加到托管视图。在处理程序方法中,决定要缩放哪个轴(如果有)并相应地调整适当的绘图范围。如果你这样做,你根本不需要绘图空间委托。

于 2012-07-19T23:40:52.070 回答
0

我正在使用 UIPinchGestureRecognizer 计算 X 和 Y 坐标的变化,然后确定绘图范围应该改变的方向(X 或 Y)。它工作正常,但不像常规缩放那样平滑,并且响应较晚。有人可以建议我一个更好的方法来做到这一点

- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer{
       if ([gestureRecognizer state] == UIGestureRecognizerStateBegan){

        CGPoint translation = [gestureRecognizer locationInView:hostView];
       NSLog(@"Sender value %f %f", translation.x,translation.y );
        initialX = translation.x;
        initialY = translation.y;        
        return;   
} 
else if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged){
                NSLog(@"inside else");
        CGPoint currentTouchLocation = [gestureRecognizer locationInView:hostView];
        NSLog(@"currentTouchLocation = %f and  %f and ",currentTouchLocation.x, currentTouchLocation.y);
            finalX = currentTouchLocation.x;
            finalY = currentTouchLocation.y;
                  }
}
-(CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate
{
    float x = fabsf(finalX - initialX) ;
    float y = fabsf(finalY - initialY);
    NSLog(@"pinch x = %f  pinch y = %f", x, y);
      CPTPlotRange *updatedRange = nil;

    if (x > y) {
        switch ( coordinate ) {
            case CPTCoordinateX:
                NSLog(@"x is greater than y change x-range");

                if (newRange.locationDouble < 0.0F ) {
                    CPTMutablePlotRange *mutableRange = [[newRange mutableCopy] autorelease];
                  mutableRange.location = CPTDecimalFromFloat(0.0);
                    updatedRange = mutableRange;
                }
                else {
                    updatedRange = newRange;
               }
                break;
            case CPTCoordinateY:
                NSLog(@"x is greater than y keep y range constant");

                updatedRange = ((CPTXYPlotSpace *)space).yRange;
                break;

        }

            }

    if (x < y) {
        switch ( coordinate ) {
            case CPTCoordinateX:
                NSLog(@"y is greater than x keep x-range constant");

                updatedRange = ((CPTXYPlotSpace *)space).xRange;
                                break;
            case CPTCoordinateY:
                if (newRange.locationDouble < 0.0F) {
                    NSLog(@"y is greater than x increase y range");
                    CPTMutablePlotRange *mutableRange = [[newRange mutableCopy] autorelease];
                  //  mutableRange.location = CPTDecimalFromFloat(0.0);
                    updatedRange = mutableRange;
                }
                else {
                   updatedRange = newRange;
                }

                break;

        }

    }
    if (x == y) {
        switch ( coordinate ) {
            case CPTCoordinateX:
                NSLog(@"y is equal to  x keep x-range constant");
                updatedRange = ((CPTXYPlotSpace *)space).xRange;
                break;
            case CPTCoordinateY:
                NSLog(@"y is equal to x keep y-range constant");
                //NSLog(@"%d", CPTCoordinateY);
                updatedRange = ((CPTXYPlotSpace *)space).yRange;
                break;

        }

    }



      return updatedRange;



}
于 2012-07-27T15:32:33.983 回答