UIView
,它在覆盖后绘制一些东西。- (void)drawRect:(CGRect)rect
这在视网膜屏幕上可以很好地工作并提供清晰的结果。
但是,现在我想使绘画基于的属性具有动画效果。创建可动画属性似乎只能在CALayer
上实现,因此我不再使用UIView
进行绘画,而是创建一个自定义的CALayer
子类并在其中绘制。
- (void) drawInContext:(CGContextRef)ctx
在这个函数中,我使用的绘图代码与自定义 UIView
的 drawRect
函数中使用的几乎完全相同。
结果看起来一样 - 但它不是retina分辨率,而是像素化的(大正方形像素)
如果我放置
self.contentsScale = [UIScreen mainScreen].scale;
在我的drawInContext
实现的开头,如果出现了像素化的结果,那么我会得到一个模糊的结果(就好像渲染仍然是以非Retina分辨率进行的,然后被放大到Retina分辨率)。
在CALayer
的drawInContext
中,如何正确地呈现清晰的Retina路径?
这里有一些屏幕截图(蓝线是所讨论的自定义绘制部分。黄色部分只是一张图片)
在自定义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);
}
为了重申我想要达到的目标:我希望在使用CALayer
渲染时实现与UIView
方法相同的清晰的视网膜渲染效果。
self.contentsScale = [UIScreen mainScreen].scale
移动到图层初始化方法中会怎样? - orkenstein