0

如何在缩放时像 CALayer 一样流畅地绘制?

如第三张(最高)图像所示,白色边框是通过准确的anti-aliased渲染绘制的。

然而,我的标签(即使我设置了contentScaleFactor)和我的手绘圆圈(即使我打开anti aliasing)都不是。

我怎样才能为 UIKit 组件或手绘图形进行抗锯齿渲染,这些图形可以像 CALayer 一样平滑地缩放比例?

有什么魔力?

小的 中等的 大的

白色边框是使用视图的 CALayer 绘制的:

    self.layer.cornerRadius = frame.size.width / 2.0f ;
    self.layer.borderColor = [UIColor whiteColor].CGColor ;
    self.layer.borderWidth = 3.0f ;

这些数字是 UILabel 的

    CGFloat contentScale = self.zoomScale * [UIScreen mainScreen].scale; // Handle retina
    label.contentScaleFactor = contentScale;

并给出:

    typedef struct {
        CGRect      rect ;
        CGColorRef  color ;
    } EventColor ;

4 个圆圈是使用 CoreGraphics 绘制的

    ::CGContextSetAllowsAntialiasing(context, true) ;
    ::CGContextSetShouldAntialias(context, true) ;
    // ...
    EventColor ec = ...
    // ...
    ::CGContextSetFillColorWithColor(context, ec.color) ;
    ::CGContextFillEllipseInRect(context, ec.rect) ;
4

1 回答 1

0

原来我误解了contentScaleFactor它的意思,发现我真正追求的是 CALayer 的contentScale.

结果是,drawRect任何 UIView 子类中的方法都可以继续并假装缩放比例始终设置为 1.0,前提是视图层的 contentScale 设置为反映滚动视图的当前缩放级别。

- (void) scrollViewDidEndZooming: (UIScrollView *) scrollView
                        withView: (UIView *) view
                         atScale: (float) scale {

    scale *= [UIScreen mainScreen].scale ;
    for (UIView * v in view.subviews) {
        v.layer.contentsScale = scale ;
        [v setNeedsDisplay] ;
    }
}

上面的例子假设滚动视图包含一个不透明的容器视图,该容器视图又包含layer.contentScale需要调整的每个实际视图。

于 2013-04-13T21:16:14.840 回答