use*_*931 19 core-graphics calayer ios retina
我有一个习惯UIView,可以覆盖一些东西
- (void)drawRect:(CGRect)rect
Run Code Online (Sandbox Code Playgroud)
这工作正常,并在视网膜屏幕上给出了明显的效果.
但是,现在我想使绘图所基于的属性具有动画效果.创建可动画的属性似乎只能在CALayer,因此我不是在绘图中UIView创建自定义CALayer子类并在里面进行绘制
- (void) drawInContext:(CGContextRef)ctx
Run Code Online (Sandbox Code Playgroud)
我在这个函数中使用drawRect了与自定义函数中使用的完全相同的绘图代码UIView.
结果看起来一样 - 然而,它不是视网膜分辨率而是像素化(大方形像素)
如果我放
self.contentsScale = [UIScreen mainScreen].scale;
Run Code Online (Sandbox Code Playgroud)
在我的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);
}
Run Code Online (Sandbox Code Playgroud)
重申我想要实现的目标:我希望实现与方法中相同的清晰视网膜渲染UIView,但是在渲染时CALayer
Ash*_*Ash 11
这个问题最有可能是contentScale; 请注意,如果您通过覆盖其layerClass功能将其分配给自定义视图,则可以重置图层的内容比例.可能还有其他一些情况也会发生这种情况.为安全起见,只有在将图层添加到视图后才设置内容比例.
尝试在自定义视图的init方法期间将主屏幕的比例分配给自定义图层.在Swift 3中看起来像这样:
layer.contentsScale = UIScreen.mainScreen().scale
Run Code Online (Sandbox Code Playgroud)
或者,在Swift 4中:
layer.contentsScale = UIScreen.main.scale
Run Code Online (Sandbox Code Playgroud)
将图层添加到其超级图层后。将 shouldRasterize 设置为 YES ,将contentsScale和resterizatioinScale设置为屏幕比例:
[self.progressView.layer addSublayer:self.progressLayer];
self.progressLayer.shouldRasterize = YES;
self.progressLayer.contentsScale = kScreenScale;
self.progressLayer.rasterizationScale = kScreenScale;
CABasicAnimation *animate = [CABasicAnimation animationWithKeyPath:@"progress"];// progress is a customized property of progressLayer
animate.duration = 1.5;
animate.beginTime = 0;
animate.fromValue = @0;
animate.toValue = @1;
animate.fillMode = kCAFillModeForwards;
animate.removedOnCompletion = NO;
animate.repeatCount = HUGE_VALF;
[self.progressLayer addAnimation:animate forKey:@"progressAnimation"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6510 次 |
| 最近记录: |