我对 iPhone 的 CALayer 绘图有一个奇怪的问题。我有一个根层,它添加了一堆代表“气泡”的子层。最终结果应该是这样的:
http://www.expensivedna.com/IMG_0018.PNG http://www.expensivedna.com/IMG_0018.PNG
问题是我似乎无法使图层抗锯齿(注意气泡上的锯齿)。我为气泡 CALayer 覆盖 drawInContext 的代码如下:
- (void)drawInContext:(CGContextRef)theContext{
CGContextSetAllowsAntialiasing(theContext, true);
CGContextSetShouldAntialias(theContext, true);
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 1.0, 1.0, 1.0, 0.5, // Start color
1.0, 1.0, 1.0, 0.0 }; // End color
CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef glossGradient =
CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
CGPoint topCenter = CGPointMake(25, 20);
CGPoint midCenter = CGPointMake(25, 25);
CGContextDrawRadialGradient(theContext, glossGradient, midCenter, 20, topCenter, 10, kCGGradientDrawsAfterEndLocation);
}
现在真正奇怪的是,如果我稍微改变绘图代码只绘制正常的红色圆圈,如下所示:
- (void)drawInContext:(CGContextRef)theContext{
CGContextSetAllowsAntialiasing(theContext, true);
CGContextSetShouldAntialias(theContext, true);
CGContextSetFillColorWithColor(theContext, [UIColor redColor].CGColor);
CGContextFillEllipseInRect(theContext, CGRectMake(0, 0, 40,40));
}
一切似乎都可以抗锯齿:
http://www.expensivedna.com/IMG_0017.PNG http://www.expensivedna.com/IMG_0017.PNG
我似乎无法弄清楚这种看似奇怪的行为。我是否错过了抗锯齿渐变和法线圆之间的一些区别?
多谢你们。