伙计们,
在编写几个刻度盘和滑块(例如,一个可以旋转的大音量按钮)时 - 我发现标准 CGContextAddArc() 使用如下:
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
CGContextSetLineWidth(ctx, radius * (KE-KR)+8);
CGContextSetStrokeColorWithColor(ctx,self.foregroundColor.CGColor);
.... more some colour/width/etc settings
...
CGContextAddArc(ctx, dx,dy,radius, 0, 2*M_PI, 0);
慢得令人难以置信。
在 iPad 上 - 带有少量填充/描边的圆圈,在拖动过程中不到 10 次干净的 [self setNeedsDisplay] 更新/秒。一个非常快速的手绘圆圈(如下所示)的破解速度要快几个数量级。同样适用于模拟器。
为什么是这样。似乎是正常填充和各种渐变填充的情况。我究竟做错了什么 ?
德。
// Stupid replacement for CGContectAddArc() which seems to be very slow.
//
void CGContextAddCirlce(CGContextRef ctx, float ox, float oy, float radius)
{
double len = 2 * M_PI * radius;
double step = 1.8 / len; // over the top :)
// translating/scaling would more efficient, etc..
//
float x = ox + radius;
float y = oy;
// stupid hack - should just do a quadrant and mirror twice.
//
CGContextMoveToPoint(ctx,x,y);
for(double a = step; a < 2.0 * M_PI -step; a += step) {
x = ox + radius * cos(a);
y = oy + radius * sin(a);
CGContextAddLineToPoint(ctx, x, y);
};
CGContextClosePath(ctx);
};