iphone - CALayer 的抗锯齿对角线边缘

标签 iphone ios core-animation calayer antialiasing

当我使用 CATransform3DRotate 设置我的 CALayer 的变换属性时,层被正确旋转。但是,该层的边缘呈锯齿状且未消除锯齿。我读过几篇关于这个主题的文章:

iPhone: CALayer + rotate in 3D + antialias?

iPhone - Jagged Edges when applying perspective to CALayer

我尝试通过在我的对角线层上设置以下属性来采纳他们的建议

CALayer* layer = myView.layer;
layer.shadowOpacity = 0.01;
layer.edgeAntialiasingMask = kCALayerTopEdge | kCALayerBottomEdge | kCALayerRightEdge | kCALayerLeftEdge;
layer.borderWidth = 1.0;
layer.borderColor = [UIColor clearColor].CGColor;
layer.backgroundColor = [UIColor greenColor].CGColor;

我还覆盖了对角线层的 drawLayer:inContext: 方法以确保消除锯齿:

-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
{
    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetShouldAntialias(context, true);
    CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
    CGContextFillRect(context, self.bounds);
}

我错过了什么?

最佳答案

从 iOS7 开始,这现在终于可以在每层的基础上实现,并且没有任何像透明像素这样的丑陋 hack 或像光栅化这样的变通方法:

layer.allowsEdgeAntialiasing = YES;

有趣的是,官方 CALayer 文档并未涵盖这一点,但绝对是一个公共(public) API。查看iOS7 API diffs和 Peter Steinberger 关于 Hidden Gems and Workarounds in iOS7 的精彩文章.

关于iphone - CALayer 的抗锯齿对角线边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6245276/

相关文章:

ios - Objective-C iOS 应用程序在 CFStringAppend 期间崩溃,原因不明

ios - CABasicAnimation 在隐式动画之后运行

ios - 夸大 CATransform3D Perspective 的透视图

macos - CABasicAnimation 过于冗长

ios - 动画 barTintColor 推送 segue

iphone - 以特定风格在 iPad 上播放视频

iphone - 尽管应用程序在前台,但 UIApplicationState 返回 Inactive

ios - 从我启动的作业中收到错误代码 126

ios - 如何在复杂的SpriteKit仿真中解决不必要的碰撞?

iphone - iOS 上每个线程可以有多个运行循环吗?