iOS UI 开发中,我们已接触过不少 layer 相关设置,如常见设置按钮的圆角效果
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.layer.cornerRadius = 5.0;
button.layer.masksToBounds = YES;
还有边框、阴影也都可以通过 CALayer 相关属性直接设置。今天要说的是一个渐变效果,举个栗子,在一些直播中我们可以看到聊天室上边有个透明度渐变效果,如下
这个时候没有办法直接通过 layer 属性配置出这种效果,而且 layer 配置的效果只能作用于当前视图,无法作用到子视图上,在这里透明度渐变实际需要作用到聊天室的 cell 上,即它的子视图,所以此时需要通过另一种方式设置。
iOS QuartzCore 框架中提供了一个 CAGradientLayer 类(CALayer 子类)用来设置渐变色,同时 UIView 视图类的 layer 属性也有个 mask 蒙层属性。官方的解释
/* A layer whose alpha channel is used as a mask to select between the
* layer's background and the result of compositing the layer's
* contents with its filtered background. Defaults to nil. When used as
* a mask the layer's `compositingFilter' and `backgroundFilters'
* properties are ignored. When set