1

我正在使用核心图形在 drawRect 方法中的 UIView 内创建一个自定义图形。我遇到的问题是在用户拖动手指的情况下移动该图表。例如,如果我的图表有 500 个点,而屏幕只有 320 像素宽,我只能显示这些点的子集,其余的点可以通过触摸和拖动来访问以查看图表的其余部分看到这张图片

我面临的问题是被感动了,这使得拖动图表相当不稳定,并且不像我想要的那样快速和响应。除了使用 CoreGraphics 之外,我对其他建议持开放态度,但这似乎是最佳选择。请在下面查看我的 drawRect 方法,并提前致谢!

- (void)drawRect:(CGRect)rect {
//[self initGraph];

// grab the current view graphics context
// the context is basically our invisible canvas that we draw into.
CGContextRef context    = UIGraphicsGetCurrentContext();
CGContextClearRect( context , [self bounds] );

CGMutablePathRef path = CGPathCreateMutable();
int spread = 1;
spread = (int)ceil((double)self.bounds.size.width/(double)[graphPoints count]);
int xTrace = 0;
int firstX = 0;
int firstY = 0; 
int count=0;
for(NSDecimalNumber *aPoint in graphPoints){
    int yPos = [self priceToPoint:aPoint];
    if(yPos < 0){
        yPos = 1;
    }
    if(count==0){
        firstX = xTrace;
        firstY = yPos;
        CGPathMoveToPoint(path, NULL, (xTrace+xOffset)+lastOffset, yPos);
    }else{
        CGPathAddLineToPoint(path, NULL, (xTrace+xOffset)+lastOffset, yPos);
    }
    xTrace = xTrace+spread;
    count++;
}
xTrace = xTrace-spread;
CGPathAddLineToPoint(path, NULL, (xTrace+xOffset)+lastOffset, self.bounds.size.height);
CGPathAddLineToPoint(path, NULL, 0, self.bounds.size.height);
CGPathAddLineToPoint(path, NULL, firstX,firstY);
// setup the gradient
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = {
    0.0/255.0, 94.0/255.0, 143.0/255.0, 1.0,  // Start color
    61.0/255.0, 110.0/255.0, 135.0/255.0, 1.0   // End color
};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradientFill = CGGradientCreateWithColorComponents (colorSpace, components, locations, 2);

// setup gradient points
CGRect pathRect = CGPathGetBoundingBox(path);
CGPoint myStartPoint, myEndPoint;
myStartPoint.x = CGRectGetMinX(pathRect);
myStartPoint.y = CGRectGetMinY(pathRect);
myEndPoint.x = CGRectGetMaxX(pathRect);
myEndPoint.y = CGRectGetMinY(pathRect);

// draw the gradient
CGContextAddPath(context, path);
CGContextSaveGState(context);
CGContextClip(context);
CGContextDrawLinearGradient (context, gradientFill, myStartPoint, myEndPoint, 0);
CGContextRestoreGState(context);

// draw the dash
CGContextAddPath(context, path);
CGContextSetLineWidth(context, 2.0);
CGContextSetRGBStrokeColor(context,87.0/255.0,155.0/255.0,191.0/255.0,0.8);
CGContextStrokePath(context);

// cleanup
CGColorSpaceRelease(colorSpace);
CGGradientRelease(gradientFill);
CGPathRelease(path);

}

4

0 回答 0