記錄幾個 CALayer 的重要屬性 (二)

記錄幾個 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的.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章