ios - 平滑 Core Graphics 曲线

标签 ios core-graphics calayer

我正在尝试创建一个圆形加载栏,并希望使用 Core Graphics 而不是 mask 图像来创建效果。但是,我没有得到我所希望的保真度:enter image description here

无论我尝试过什么,锯齿看起来都很糟糕。抗锯齿功能已开启,我已经尝试在 CGContextRef 上将“平坦度”调低至 .1。该图像来自(Retina)模拟器,它在设备上看起来稍微好一些,但仍然不是那么好。

绘图代码,在 CALayer 的子类中:

-(void)drawInContext:(CGContextRef)ctx {    
    CGPoint center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);

    CGFloat delta = toRadians(360 * percent);

    CGFloat innerRadius = 69.5;
    CGFloat outerRadius = innerRadius + 12;

    CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:99/256.0 green:183/256.0 blue:70/256.0 alpha:.5].CGColor);
    CGContextSetStrokeColorWithColor(ctx, [UIColor colorWithRed:99/256.0 green:183/256.0 blue:70/256.0 alpha:10.0].CGColor);
    CGContextSetLineWidth(ctx, 1);

    CGMutablePathRef path = CGPathCreateMutable();

    CGPathAddRelativeArc(path, NULL, center.x, center.y, innerRadius, -(M_PI / 2), delta);
    CGPathAddRelativeArc(path, NULL, center.x, center.y, outerRadius, delta - (M_PI / 2), -delta);
    CGPathAddLineToPoint(path, NULL, center.x, center.y-innerRadius);

    CGContextAddPath(ctx, path);
    CGContextFillPath(ctx);
    CGContextAddPath(ctx, path);
    CGContextStrokePath(ctx);
}

这是 Core Graphics 能做的最好的还是我遗漏了什么?

最佳答案

找出我的具体问题。由于我正在创建一个 CALayer(作为子层添加),contentScale 不会自动设置为 Retina 显示。这解决了它:

percentLayer.contentsScale = [UIScreen mainScreen].scale;

感谢 Rob,他将我推向了正确的方向。

关于ios - 平滑 Core Graphics 曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8810491/

相关文章:

iphone - 从 NSData 或 UIImage 查找图像类型

ios - 为各种函数创建一个数组线程安全 - iOS

macos - 从 CGImageRef 获取 NSImage

iOS : need inputs in developing efficient ( performance wise ) drawing app

iphone - iOS 中的自定义图像 mask

ios - 移除特定节点的 Sprite 套件

ios - iOS7 中 UIAlertView 的葫芦问题

ios - 为什么在UIView上画线需要背景色?

ios - 如何从 CGContext 获取颜色空间?

ios - swift : Set anchor Point on CABasicAnimation