在 iOS 中渲染颜色均匀的不透明非渐变圆形时,似乎有三种可能的技术:
使用像 circle-icon.png
和 circle-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 种方法在性能和维护方面的确切权衡是什么?
最佳答案
您忽略了另一个非常合乎逻辑的选择,UIBezierPath
和 CAShapeLayer
.创建一个圆形的 UIBezierPath
,创建一个使用该 UIBezierPath
的 CAShapeLayer
,然后将该图层添加到您的 View /图层层次结构中。
其次,导入适当的 header :
#import <QuartzCore/QuartzCore.h>
然后你可以添加一个
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/