美文网首页
iOS给image/view增加阴影

iOS给image/view增加阴影

作者: TsingQue | 来源:发表于2016-03-30 13:04 被阅读3763次

shadow属性 from Xcode

这是从xcode里面摘取的内容,苹果的官方文档
/** Shadow properties. **/

/* The color of the shadow. Defaults to opaque black. Colors created
* from patterns are currently NOT supported. Animatable. */

@property(nullable) CGColorRef shadowColor;
/* The opacity of the shadow. Defaults to 0. Specifying a value outside the
* [0,1] range will give undefined results. Animatable. */
@property float shadowOpacity;

/* The shadow offset. Defaults to (0, -3). Animatable. */
@property CGSize shadowOffset;
/* The blur radius used to create the shadow. Defaults to 3. Animatable. */
@property CGFloat shadowRadius;
/* 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. Upon assignment the path is copied.
  • Defaults to null. Animatable. */

@property(nullable) CGPathRef shadowPath;`

其实就是三个属性

//加阴影
_imageView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
_imageView.layer.shadowOffset = CGSizeMake(4,4);//shadowOffset阴影偏移,x向右偏移4,y向下偏移4,默认(0, -3),这个跟shadowRadius配合使用
_imageView.layer.shadowOpacity = 0.8;//阴影透明度,默认0

_imageView.layer.shadowRadius = 4;//阴影半径,默认3

后面这个是做的效果图
form http://blog.csdn.net/rhljiayou/article/details/10178723

_imageView1.layer.shadowColor = [UIColor yellowColor].CGColor;//shadowColor阴影颜色
_imageView1.layer.shadowOffset = CGSizeMake(0,0);//shadowOffset阴影偏移,默认(0, -3),这个跟shadowRadius配合使用
_imageView1.layer.shadowOpacity = 1;//阴影透明度,默认0
_imageView1.layer.shadowRadius = 3;//阴影半径,默认3

//路径阴影
UIBezierPath *path = [UIBezierPath bezierPath];

float width = _imageView1.bounds.size.width;
float height = _imageView1.bounds.size.height;
float x = _imageView1.bounds.origin.x;
float y = _imageView1.bounds.origin.y;
float addWH = 10;

CGPoint topLeft      = _imageView1.bounds.origin;
CGPoint topMiddle = CGPointMake(x+(width/2),y-addWH);
CGPoint topRight     = CGPointMake(x+width,y);

CGPoint rightMiddle = CGPointMake(x+width+addWH,y+(height/2));

CGPoint bottomRight  = CGPointMake(x+width,y+height);
CGPoint bottomMiddle = CGPointMake(x+(width/2),y+height+addWH);
CGPoint bottomLeft   = CGPointMake(x,y+height);


CGPoint leftMiddle = CGPointMake(x-addWH,y+(height/2));

[path moveToPoint:topLeft];
//添加四个二元曲线
[path addQuadCurveToPoint:topRight
             controlPoint:topMiddle];
[path addQuadCurveToPoint:bottomRight
             controlPoint:rightMiddle];
[path addQuadCurveToPoint:bottomLeft
             controlPoint:bottomMiddle];
[path addQuadCurveToPoint:topLeft
             controlPoint:leftMiddle];
//设置阴影路径
 _imageView1.layer.shadowPath = path.CGPath;

相关文章

网友评论

      本文标题:iOS给image/view增加阴影

      本文链接:https://www.haomeiwen.com/subject/xxaslttx.html