Add shadow to UIView

效果图:

以下代码实现:

第一个图片的代码

  1. //加阴影--任海丽编辑   
  2.     _imageView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色   
  3.     _imageView.layer.shadowOffset = CGSizeMake(4,4);//shadowOffset阴影偏移,x向右偏移4,y向下偏移4,默认(0, -3),这个跟shadowRadius配合使用   
  4.     _imageView.layer.shadowOpacity = 0.8;//阴影透明度,默认0   
  5.     _imageView.layer.shadowRadius = 4;//阴影半径,默认3  
//加阴影--任海丽编辑
    _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



第二个图片的代码

  1. _imageView1.layer.shadowColor = [UIColor yellowColor].CGColor;//shadowColor阴影颜色   
  2. _imageView1.layer.shadowOffset = CGSizeMake(0,0);//shadowOffset阴影偏移,默认(0, -3),这个跟shadowRadius配合使用   
  3. _imageView1.layer.shadowOpacity = 1;//阴影透明度,默认0   
  4. _imageView1.layer.shadowRadius = 3;//阴影半径,默认3   
  5.   
  6. //路径阴影   
  7. UIBezierPath *path = [UIBezierPath bezierPath];  
  8.   
  9. float width = _imageView1.bounds.size.width;  
  10. float height = _imageView1.bounds.size.height;  
  11. float x = _imageView1.bounds.origin.x;  
  12. float y = _imageView1.bounds.origin.y;  
  13. float addWH = 10;  
  14.   
  15. CGPoint topLeft      = _imageView1.bounds.origin;  
  16. CGPoint topMiddle = CGPointMake(x+(width/2),y-addWH);  
  17. CGPoint topRight     = CGPointMake(x+width,y);  
  18.   
  19. CGPoint rightMiddle = CGPointMake(x+width+addWH,y+(height/2));  
  20.   
  21. CGPoint bottomRight  = CGPointMake(x+width,y+height);  
  22. CGPoint bottomMiddle = CGPointMake(x+(width/2),y+height+addWH);  
  23. CGPoint bottomLeft   = CGPointMake(x,y+height);  
  24.   
  25.   
  26. CGPoint leftMiddle = CGPointMake(x-addWH,y+(height/2));  
  27.   
  28. [path moveToPoint:topLeft];  
  29. //添加四个二元曲线   
  30. [path addQuadCurveToPoint:topRight  
  31.              controlPoint:topMiddle];  
  32. [path addQuadCurveToPoint:bottomRight  
  33.              controlPoint:rightMiddle];  
  34. [path addQuadCurveToPoint:bottomLeft  
  35.              controlPoint:bottomMiddle];  
  36. [path addQuadCurveToPoint:topLeft  
  37.              controlPoint:leftMiddle];  
  38. //设置阴影路径   
  39.  _imageView1.layer.shadowPath = path.CGPath;  
    _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;


ok!

猜你喜欢

转载自shappy1978.iteye.com/blog/2146100
Add