沿Y轴和X轴独立缩放核心图

时间:2023-01-12 20:19:10

I am Using the below code to scroll my core plot along X-axis Y-axis and zoom. Its working fine. But I when i zoom my core plot, it zooms in both the directions. I want the plot to zoom along X if I pinch along x-direction and zoom Y if pinch along Y-direction. Can someone help me with this please.

我使用下面的代码沿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;
}

2 个解决方案

#1


1  

The easiest way to keep the axes and title in the correct position is to use the axisConstraints for the axes and leave the titleLocation at it's default of NAN. This will relieve your delegate of responsibility for updating those items and you can focus on the zooming.

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

Of those two delegate methods, you only need -plotSpace:willChangePlotRangeTo:forCoordinate:. The other one is only called when scrolling.

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

Decide whether you want to allow the zoom to happen in x or y (see the links in the comments on the original question). Check the coordinate parameter in the delegate method; return the newRange to allow the zoom to happen or [space plotRangeForCoordinate:coordinate] to restore the original range and prevent zooming.

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

If you need to use your own gesture recognizer to detect the pinch angle, set allowPinchScaling to NO on the hosting view to disable the built-in recognizer. Add your own recognizer to the hosting view. In the handler method, decide which axis to scale (if any) and adjust the appropriate plot range accordingly. If you do it this way, you don't need a plot space delegate at all.

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

#2


0  

I am calculating the change in X and Y coordinates using UIPinchGestureRecognizer and then determining which direction the plot range should change(either X or Y). Its working fine but It is not as smooth as regular zoom, and it responds late. Can someone suggest me a better way to do this

我正在使用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;



}

#1


1  

The easiest way to keep the axes and title in the correct position is to use the axisConstraints for the axes and leave the titleLocation at it's default of NAN. This will relieve your delegate of responsibility for updating those items and you can focus on the zooming.

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

Of those two delegate methods, you only need -plotSpace:willChangePlotRangeTo:forCoordinate:. The other one is only called when scrolling.

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

Decide whether you want to allow the zoom to happen in x or y (see the links in the comments on the original question). Check the coordinate parameter in the delegate method; return the newRange to allow the zoom to happen or [space plotRangeForCoordinate:coordinate] to restore the original range and prevent zooming.

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

If you need to use your own gesture recognizer to detect the pinch angle, set allowPinchScaling to NO on the hosting view to disable the built-in recognizer. Add your own recognizer to the hosting view. In the handler method, decide which axis to scale (if any) and adjust the appropriate plot range accordingly. If you do it this way, you don't need a plot space delegate at all.

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

#2


0  

I am calculating the change in X and Y coordinates using UIPinchGestureRecognizer and then determining which direction the plot range should change(either X or Y). Its working fine but It is not as smooth as regular zoom, and it responds late. Can someone suggest me a better way to do this

我正在使用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;



}