Quartz 2D Programming Guide --Transparency Layers

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

参考

[1]http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_trans_layers/dq_trans_layers.html#//apple_ref/doc/uid/TP30001066-CH210-TPXREF101


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

应用透明层的阴影如下:


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




透明层跟普通的层一样,都是独立的实体。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,效果如下:








评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值