调用touchesMoved时,带有Core Graphics和波形帧速率的自定义图形

时间:2011-11-23 20:12:27

标签: iphone performance core-graphics graphing touchesmoved

我正在使用核心图形在drawRect方法中的UIView内创建自定义图形。我遇到的问题是用户拖动手指移动该图形。例如,如果我的图表有500个点且屏幕只有320个像素宽,我只能显示这些点的子集,其余部分可以在触摸和拖动时访问,以查看图表的其余部分See this image < / p>

我面临的问题是触及移动使拖动图形相当不稳定,并没有像我想的那样快速和响应。除了使用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);

}

0 个答案:

没有答案