猎豆优选

UIView+CCShadow.m 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // UIView+CCShadow.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/11/20.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "UIView+CCShadow.h"
  9. @implementation UIView (CCShadow)
  10. - (void)addShadowWithOpacity:(float)shadowOpacity
  11. shadowRadius:(CGFloat)shadowRadius
  12. andCornerRadius:(CGFloat)cornerRadius
  13. andShadowColor:(UIColor *)color
  14. {
  15. //////// shadow /////////
  16. CALayer *shadowLayer = [CALayer layer];
  17. shadowLayer.frame = self.layer.frame;
  18. shadowLayer.shadowColor = color.CGColor;//shadowColor阴影颜色
  19. shadowLayer.shadowOffset = CGSizeMake(0, 0);//shadowOffset阴影偏移,默认(0, -3),这个跟shadowRadius配合使用
  20. shadowLayer.shadowOpacity = shadowOpacity;//0.8;//阴影透明度,默认0
  21. shadowLayer.shadowRadius = shadowRadius;//8;//阴影半径,默认3
  22. //路径阴影
  23. UIBezierPath *path = [UIBezierPath bezierPath];
  24. float width = shadowLayer.bounds.size.width;
  25. float height = shadowLayer.bounds.size.height;
  26. float x = shadowLayer.bounds.origin.x;
  27. float y = shadowLayer.bounds.origin.y;
  28. CGPoint topLeft = shadowLayer.bounds.origin;
  29. CGPoint topRight = CGPointMake(x + width, y);
  30. CGPoint bottomRight = CGPointMake(x + width, y + height);
  31. CGPoint bottomLeft = CGPointMake(x, y + height);
  32. CGFloat offset = -1.f;
  33. [path moveToPoint:CGPointMake(topLeft.x - offset, topLeft.y + cornerRadius)];
  34. [path addArcWithCenter:CGPointMake(topLeft.x + cornerRadius, topLeft.y + cornerRadius) radius:(cornerRadius + offset) startAngle:M_PI endAngle:M_PI_2 * 3 clockwise:YES];
  35. [path addLineToPoint:CGPointMake(topRight.x - cornerRadius, topRight.y - offset)];
  36. [path addArcWithCenter:CGPointMake(topRight.x - cornerRadius, topRight.y + cornerRadius) radius:(cornerRadius + offset) startAngle:M_PI_2 * 3 endAngle:M_PI * 2 clockwise:YES];
  37. [path addLineToPoint:CGPointMake(bottomRight.x + offset, bottomRight.y - cornerRadius)];
  38. [path addArcWithCenter:CGPointMake(bottomRight.x - cornerRadius, bottomRight.y - cornerRadius) radius:(cornerRadius + offset) startAngle:0 endAngle:M_PI_2 clockwise:YES];
  39. [path addLineToPoint:CGPointMake(bottomLeft.x + cornerRadius, bottomLeft.y + offset)];
  40. [path addArcWithCenter:CGPointMake(bottomLeft.x + cornerRadius, bottomLeft.y - cornerRadius) radius:(cornerRadius + offset) startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
  41. [path addLineToPoint:CGPointMake(topLeft.x - offset, topLeft.y + cornerRadius)];
  42. //设置阴影路径
  43. shadowLayer.shadowPath = path.CGPath;
  44. //////// cornerRadius /////////
  45. self.layer.cornerRadius = cornerRadius;
  46. self.layer.masksToBounds = YES;
  47. self.layer.shouldRasterize = YES;
  48. self.layer.rasterizationScale = [UIScreen mainScreen].scale;
  49. [self.superview.layer insertSublayer:shadowLayer below:self.layer];
  50. }
  51. @end