• 问题总会出现,不过解决问题的方法也会出现!!!

为UIView设置阴影(CALayer的shadowColor,shadowOffset,shadowOpacity,shadowRadius,shadowPath属性)

QuartzCore 小雨 来源:任海丽 1216次浏览 已收录 0个评论

为UIView设置阴影(CALayer的shadowColor,shadowOffset,shadowOpacity,shadowRadius,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 

第二个图片的代码

_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;

本博客内容既有转载自网络的内容,也有本作者原创内容,仅供学习与交流之用
如有侵权或者错误之处,请及时在下方留言!
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址