我有一个习惯UIView
,可以在覆盖中绘制一些东西
- (void)drawRect:(CGRect)rect
这工作正常,并在视网膜屏幕上提供清晰的结果。
但是,现在我想使绘图所基于的属性可动画化。创建动画属性似乎只能在CALayer
.UIView
CALayer
- (void) drawInContext:(CGContextRef)ctx
我在这个函数中使用了与我在drawRect
custom 函数中使用的几乎完全相同的绘图代码UIView
。
结果看起来一样 - 但是,它不是视网膜分辨率,而是像素化(大方形像素)
如果我把
self.contentsScale = [UIScreen mainScreen].scale;
在我的实现开始时drawInContext
,我得到的不是像素化结果,而是模糊的结果(好像渲染仍然以非视网膜分辨率执行,然后放大到视网膜分辨率)。
渲染锐利视网膜路径的正确方法是CALayers
drawInContext
什么?
这是一些截图(蓝线是有问题的自定义绘图的一部分。黄色部分只是一个图像)
在自定义 UIView 中绘制drawRect
:
在自定义 CALayer 中绘制drawInContext
:
在自定义 CALayer 中绘制drawInContext
,首先设置self.contentScale
:
为了完整起见,这里是(精简版的)绘图代码:
//if drawing inside custom UIView sublcass:
- (void)drawRect:(CGRect)rect
{
CGContextRef currenctContext = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextSetLineWidth(currenctContext, _lineWidth);
CGContextSetLineJoin(currenctContext,kCGLineJoinRound);
CGContextMoveToPoint(currenctContext,x1, y1);
CGContextAddLineToPoint(currenctContext,x2, y2);
CGContextStrokePath(currenctContext);
}
//if drawing inside custom CALayer subclass:
- (void) drawInContext:(CGContextRef)ctx {
{
//self.contentsScale = [UIScreen mainScreen].scale;
CGContextRef currenctContext = ctx;
CGContextSetStrokeColorWithColor(currenctContext, [UIColor blackColor].CGColor);
CGContextSetLineWidth(currenctContext, _lineWidth);
CGContextSetLineJoin(currenctContext,kCGLineJoinRound);
CGContextMoveToPoint(currenctContext,x1, y1);
CGContextAddLineToPoint(currenctContext,x2, y2);
CGContextStrokePath(currenctContext);
}
重申我想要实现的目标:我想要实现与UIView
方法相同的清晰视网膜渲染,但是在渲染时CALayer