参考
透明层的一个作用当对多个图形应用阴影时,能够实现多个图形的完整阴影,而不是几个阴影叠加。
应用透明层的阴影如下:

如果未应用阴影层时,效果如下:

透明层跟普通的层一样,都是独立的实体。quartz的每个context包含一个透明层stack,并且可以嵌套,但因为层经常是stack 的一部分,所以一般不能直接操作他们。
使用透明层的步骤
1.调用CGContextBeginTransparencyLayer,这之后graphic context的alpha自动设置为1,关闭阴影,blend mode 设置为默认,其他参数未变。
2.在context的其他自定义其他绘制操作。
3.调用CGContextEndTransparencyLayer,quartz最后聚合结果到context中。
以下是使用透明层的代码,在drawRect中调用
- (void)drawRect:(CGRect)rect
{ [self initTransparencyLayer];
}
- (void)initTransparencyLayer
{
CGFloat size = 100;
CGRect rect = CGRectMake(0, 0, 300, 300);
CGRect redRect = CGRectMake(10, 10, size, size);
CGRect greenRect = CGRectMake(size/2, size/2, size, size);
CGRect blueRect = CGRectMake(size, size, size, size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShadow(context, CGSizeMake(5, 10), 10);
CGContextBeginTransparencyLayerWithRect(context, rect, NULL);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillEllipseInRect(context, redRect);
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
CGContextFillEllipseInRect(context, greenRect);
CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
CGContextFillEllipseInRect(context, blueRect);
CGContextEndTransparencyLayer(context);
}
效果如下:

设置CGContextSetShadow(context, CGSizeMake(5, 10), 0);第三个参数,模糊参数为0,效果如下:

本文详细介绍了如何在Quartz 2D中利用透明层技术实现多个图形的完整阴影,包括具体步骤和代码示例。

被折叠的 条评论
为什么被折叠?



