ios - 在 iOS 中渲染圆圈的最佳技术是什么?

标签 ios objective-c rendering

在 iOS 中渲染颜色均匀的不透明非渐变圆形时,似乎有三种可能的技术:

使用像 circle-icon.pngcircle-icon@2px.png 这样的图像。然后,可以实现以下代码让 iOS 自动呈现适当的尺寸:

UIImage *image = [UIImage imageNamed:@"circle-icon"];
self.closeIcon = [[UIImageView alloc] initWithImage:image];
self.closeIcon.frame = CGRectMake(300, 16, image.size.width, image.size.height);

渲染圆角和使用层,像这样:。

self.circleView = [[UIView alloc] initWithFrame:CGRectMake(10,20,100,100)];
circleView.alpha = 0.5;
self.circleView.layer.cornerRadius = 50;
self.circleView.backgroundColor = [UIColor blueColor];

使用原生绘图库,比如 CGContextFillEllipseInRect

这 3 种方法在性能和维护方面的确切权衡是什么?

最佳答案

您忽略了另一个非常合乎逻辑的选择,UIBezierPathCAShapeLayer .创建一个圆形的 UIBezierPath,创建一个使用该 UIBezierPathCAShapeLayer,然后将该图层添加到您的 View /图层层次结构中。

  1. Add the QuartzCore framework to your project .

  2. 其次,导入适当的 header :

    #import <QuartzCore/QuartzCore.h>
    
  3. 然后你可以添加一个 CAShapeLayer 到你的 View 层:

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path addArcWithCenter:CGPointMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height / 2.0) radius:self.view.bounds.size.width * 0.40 startAngle:0 endAngle:M_PI * 2.0 clockwise:YES];
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    layer.path = [path CGPath];
    layer.fillColor = [[UIColor blueColor] CGColor];
    [self.view.layer addSublayer:layer];
    

我认为 CoreGraphics 实现或此 CAShapeLayer 实现比 PNG 文件或带圆角的 UIView 对象更有意义。

关于ios - 在 iOS 中渲染圆圈的最佳技术是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18346896/

相关文章:

android - 在 iOS 和 Android 上 react native 模态模糊/活力?

ios - Xcode : Why doesn't this code work for saving UITextField using NSUserDefaults?

iphone - Objective-C 内存问题

winapi - Windows : Mouse Down on Window Decoration

wolfram-mathematica - Mathematica 渲染和导出为不同颜色的 RGB 颜色值的最小差异是多少?

iphone - iOS HTTP 通信设置

iphone - iOS UIView 动画问题

iphone - 快速滚动时,自定义单元格中的多行标签与下一个单元格重叠

objective-c - 拖出 NSTableView 以移除

python - 协调Python Aggdraw中的容器类型以获得最快的渲染速度?