记录几个 CALayer 的重要属性 (二)

来源:互联网 发布:单片机工程师 名称 编辑:程序博客网 时间:2024/05/27 09:46

记录几个 CALayer 的重要属性:

有关阴影配置

  • shadowColor : 配置阴影颜色
@property CGColorRef shadowColor;/* The color of the shadow. Defaults to opaque black. Colors created * from patterns are currently NOT supported. Animatable. */

默认阴影颜色为黑色, 可以设置 ARGB, 默认值均是0, 可设置范围是[0, 1]之间.
typedef struct CGColor CGColorRef;//CGColorRef 就是 CGColor
shadowColor 的赋值方法如下:

subView.layer.shadowColor = [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:arc4random() % 256 / 256.0].CGColor;// 获取一个随机颜色阴影
  • shadowOpacity : 配置阴影透明效果
@property float shadowOpacity;/* The opacity of the shadow. Defaults to 0. Specifying a value outside the [0,1] range will give undefined results. Animatable. */

默认阴影不透明度为0.0, 0.0是透明的,1.0是不透明的.
shadowOpacity 的赋值方法:

subView.layer.shadowOpacity = 0.5;
  • shadowOffset : 配置阴影在 X, Y 轴上的 延伸
@property CGSize shadowOffset;/* The shadow offset. Defaults to (0, 1). Animatable. */

配置阴影在 x, y 轴方向上的延伸,默认值为(0, 1)
关于阴影方向如下:
①在 x 轴的正半轴,则阴影在 subView 的右边
②在 x 轴的负半轴,则阴影在 subView 的左边
③在 y 轴的正半轴,则阴影在 subView 的下方
④在 y 轴的负半轴,则阴影在 subView 的上方
设置方法如下:

subView.layer.shadowOffset = CGSizeMake(10, 10);
  • shadowRadius : 配置阴影的渐变距离
@property CGFloat shadowRadius;/* The blur radius used to create the shadow. Defaults to 3. Animatable. */

配置模糊半径, 默认值为3. 模糊半径越大, 阴影面积越大, 阴影越不清晰.
shadowRadius 的设置方法:

subView.layer.shadowRadius = 100;
  • shadowPath : 配置阴影的位置
@property CGPathRef shadowPath;/* When non-null this path defines the outline used to construct the * layer's shadow instead of using the layer's composited alpha * channel. The path is rendered using the non-zero winding rule. * Specifying the path explicitly using this property will usually * improve rendering performance, as will sharing the same path * reference across multiple layers. Defaults to null. Animatable. */

设置阴影路径(位置和大小), 默认值为空, 这个路径的参考坐标系是 subView 的坐标系.
这里用到 UIBezierPath, 先不做过多说明.
shadowPath 的设置方法如下:

subView.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(100, 0, 10, 10)].CGPath;//该阴影的路径是在 subView 的坐标系的 x=100,y=0的位置开始创建一个(阴影)层,阴影大小是10*10的.
0 0
原创粉丝点击