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

时间:2012-07-19 18:40:21

标签: objective-c xcode xcode4.3 core-plot

我使用下面的代码沿着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 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分: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;



}