iOS Quartz2D 透明层 TransparencyLayer

来源:互联网 发布:dva防御矩阵原理 编辑:程序博客网 时间:2024/06/06 09:26

最近在研究iOS Quartz2D图形绘制引擎----->透明层TransparencyLayer


透明层


        通过组合两个或多个对象来生成一个组合图形, 组合图形被看成是单一对象, 当需要在一组对象上使用特效的时候, 透明层非常有用
     
        透明层的工作方式
        Quartz2D的透明层类似很多流行的图形应用中的层, 层是独立的实体,
        Quartz维护为每个上下文维护一个透明栈, 并且透明层可以嵌套的, 但由于层通常是栈的一部分, 所以我们不能单独操作它们,
        通过调用CGContextBeginTransparencyLayer开始一个透明层,
        该函数有两个参数(图形上下文, CFDictionary对象)字典对象一般为NULL
        图形状态参数保持不变, 除了alpha值(默认为1),阴影(默认关闭), 混合模式(默认为normal), 及其他影响最终组合的参数.


效果:


直接上代码,注释很清楚,简单易懂

////  Quartz2dViewFour.m//  Quartz2DDemoOne////  Created by 帝炎魔 on 16/5/23.//  Copyright © 2016年 帝炎魔. All rights reserved.//#import "Quartz2dViewFour.h"@implementation Quartz2dViewFour// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect {    // Drawing code            /**     *  透明层        通过组合两个或多个对象来生成一个组合图形, 组合图形被看成是单一对象, 当需要在一组对象上使用特效的时候, 透明层非常有用             透明层的工作方式        Quartz2D的透明层类似很多流行的图形应用中的层, 层是独立的实体,        Quartz维护为每个上下文维护一个透明栈, 并且透明层可以嵌套的, 但由于层通常是栈的一部分, 所以我们不能单独操作它们,        通过调用CGContextBeginTransparencyLayer开始一个透明层,         该函数有两个参数(图形上下文, CFDictionary对象)字典对象一般为NULL        图形状态参数保持不变, 除了alpha值(默认为1),阴影(默认关闭), 混合模式(默认为normal), 及其他影响最终组合的参数.                       *  透明层的工作方式     *     *  开始透明层的操作, 可以绘制任何想显示在层上的对象, 指定上下文中的绘制操作将被当成一个组合对象慧慧到一个透明背景上, 这个背景被当做一个独立于图形上下文的目标缓存        绘制完成之后, 我们调用函数CGContextEndTransparencyLayer.        Quartz将结合对象放入上下文, 并使用上下文的全局alpha值, 阴影状态以及裁剪区域作用于组合对象.             在透明层中绘制需要三步:             1. 调用函数CGContextBeginTransparencyLayer        2. 在透明层中绘制需要组合的对象        3. 调用函数CGContextEndTransparencyLayer     */        [self transparencyLayerWithWidth:300 height:300];        }- (void)transparencyLayerWithWidth:(float)width height:(float)height{    CGContextRef context = UIGraphicsGetCurrentContext();    // 设置阴影的偏移量    CGSize myShadowOffset = CGSizeMake(10, -20);    CGContextSetShadow(context, myShadowOffset, 10);        // 开始透明层    CGContextBeginTransparencyLayer(context, NULL);        // 在透明层绘制需要组合的对象    CGContextSetRGBFillColor(context, 0, 1, 0, 1);    CGContextFillRect(context, CGRectMake(width/3 + 50, height/2, width/4, height/4));        CGContextSetRGBFillColor(context, 0, 0, 1, 1);    CGContextFillRect(context, CGRectMake(width/3 - 50, height/2 - 100, width/4, height/4));        CGContextSetRGBFillColor(context, 1, 0, 0, 1);    CGContextFillRect(context, CGRectMake(width/3, height/2 - 50, width/4, height/4));        // 绘制完成 结束透明层操作    CGContextEndTransparencyLayer(context);}@end




0 0
原创粉丝点击