CAGradientLayer 初识

来源:互联网 发布:末日数据化小说 女主 编辑:程序博客网 时间:2024/05/21 00:53


先上代码,给大家看个效果

    CAGradientLayer *layer = [CAGradientLayer layer];    layer.startPoint = (CGPoint){0.5f, 0.0f};    layer.endPoint = (CGPoint){0.5f, 1.0f};    layer.colors = [NSArray arrayWithObjects:(id)[UIColor blueColor].CGColor, (id)[UIColor redColor].CGColor, (id)[UIColor greenColor].CGColor, nil];    layer.locations = @[@0.0f, @0.6f, @1.0f];    layer.frame = self.view.layer.bounds;    [self.view.layer insertSublayer:layer atIndex:0];
效果图:(背景颜色,,,其他的忽略)






CAGradientLayer

CAGradientLayer类是用于在其背景色上绘制一个颜色渐变,以填充层的整个形状,包括圆角。这个类继承自CALayer类,使用起来还是很方便的。

与Quartz 2D中的渐变处理类似,一个渐变有一个起始位置(startPoint)和一个结束位置(endPoint),在这两个位置之间,我们可以指定一组颜色值(colors,元素是CGColorRef对象),可以是两个,也可以是多个,每个颜色值会对应一个位置(locations)。另外,渐变还分为轴向渐变和径向渐变。


@property CGPoint startPoint

映射locations中第一个位置,用单位向量表示,比如(0,0)表示从左上角开始变化。默认值是(0.5,0.0)。

@property CGPoint endPoint

映射locations中最后一个位置,用单位向量表示,比如(1,1)表示到右下角变化结束。默认值是(0.5,1.0)。

代码中 

    layer.startPoint = (CGPoint){0.5f, 0.0f};    layer.endPoint = (CGPoint){0.5f, 1.0f};
就是从上到下面



0 0
原创粉丝点击