调用 TouchMoved 时具有 Core Graphics 和不稳定帧速率的自定义图形

发布于 2024-12-17 14:37:49 字数 2613 浏览 2 评论 0原文

我正在使用核心图形的 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);

}

I am creating a custom graph inside a UIView within the drawRect method using core graphics. The problem I am having is moving that graph with the user dragging their finger. For example, if my graph has 500 points and the screen is only 320 pixels wide, I can only show a subset of those points and the rest can be accessed when touching and dragging to see the rest of the graphSee this image

The problem i am facing is touched moved makes dragging the graph fairly choppy and isn't as quick and responsive as I'd like. I am open to other suggestions besides using CoreGraphics, but it seemed like the best choice. Please see my drawRect method below and thanks in advance!

- (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);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文