UIView+RDSClip.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // UIView+RDSClip.m
  3. // singleProduct
  4. //
  5. // Created by coderYK on 2018/4/30.
  6. // Copyright © 2018年 RDIOT. All rights reserved.
  7. //
  8. #import "UIView+RDSClip.h"
  9. @implementation UIView (RDSClip)
  10. - (void)rds_setCornerRadiusWithType:(Corner_Radius_Or_BorderLine_TYPE)type Radius:(CGFloat)cornerRadius {
  11. self.layer.cornerRadius = 0;
  12. UIRectCorner rectcorners ;
  13. switch (type) {
  14. case Corner_Radius_Or_BorderLine_All:{
  15. rectcorners = UIRectCornerAllCorners;
  16. self.layer.cornerRadius = cornerRadius;
  17. self.layer.borderColor = [UIColor blackColor].CGColor;
  18. }
  19. break;
  20. case Corner_Radius_Or_BorderLine_UP:{
  21. rectcorners = UIRectCornerTopRight|UIRectCornerTopLeft;
  22. }
  23. break;
  24. case Corner_Radius_Or_BorderLine_DOWN:{
  25. rectcorners = UIRectCornerBottomLeft|UIRectCornerBottomRight;
  26. }
  27. break;
  28. case Corner_Radius_Or_BorderLine_LEFT:{
  29. rectcorners = UIRectCornerTopLeft|UIRectCornerBottomLeft;
  30. }
  31. break;
  32. case Corner_Radius_Or_BorderLine_RIGHT:{
  33. rectcorners = UIRectCornerTopRight|UIRectCornerBottomRight;
  34. }
  35. break;
  36. }
  37. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
  38. byRoundingCorners:rectcorners
  39. cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
  40. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  41. maskLayer.frame = self.bounds;
  42. maskLayer.path = maskPath.CGPath;
  43. self.layer.mask = maskLayer;
  44. self.layer.masksToBounds = YES;
  45. }
  46. - (void)rds_drawBorderLineWithType:(Corner_Radius_Or_BorderLine_TYPE)type lineWidth:(CGFloat)lineWidth lineColor:(UIColor *)lineColor {
  47. /// 线的路径
  48. UIBezierPath *bezierPath = [UIBezierPath bezierPath];
  49. if (type==Corner_Radius_Or_BorderLine_All) {
  50. self.layer.borderWidth = lineWidth;
  51. self.layer.borderColor = lineColor.CGColor;
  52. return;
  53. }
  54. else if (type==Corner_Radius_Or_BorderLine_UP) {
  55. [bezierPath moveToPoint:CGPointMake(0.0f, lineWidth)];
  56. [bezierPath addLineToPoint:CGPointMake(self.frame.size.width, lineWidth)];
  57. }
  58. else if (type==Corner_Radius_Or_BorderLine_RIGHT) {
  59. [bezierPath moveToPoint:CGPointMake(self.frame.size.width-lineWidth, 0.0f)];
  60. [bezierPath addLineToPoint:CGPointMake( self.frame.size.width-lineWidth, self.frame.size.height)];
  61. }
  62. else if (type==Corner_Radius_Or_BorderLine_DOWN) {
  63. [bezierPath moveToPoint:CGPointMake(0.0f, self.frame.size.height-lineWidth)];
  64. [bezierPath addLineToPoint:CGPointMake( self.frame.size.width, self.frame.size.height-lineWidth)];
  65. }
  66. else if (type==Corner_Radius_Or_BorderLine_LEFT) {
  67. [bezierPath moveToPoint:CGPointMake(0.0f+lineWidth, self.frame.size.height)];
  68. [bezierPath addLineToPoint:CGPointMake(0.0f+lineWidth, 0.0f)];
  69. }
  70. CAShapeLayer * shapeLayer = [CAShapeLayer layer];
  71. shapeLayer.strokeColor = lineColor.CGColor;
  72. shapeLayer.fillColor = [UIColor clearColor].CGColor;
  73. /// 添加路径
  74. shapeLayer.path = bezierPath.CGPath;
  75. /// 线宽度
  76. shapeLayer.lineWidth = lineWidth;
  77. [self.layer insertSublayer:shapeLayer atIndex:0];
  78. self.layer.masksToBounds = YES;
  79. }
  80. - (void)rds_setCornerRadius:(CGFloat)cornerRadius borderLineWidth:(CGFloat)lineWidth lineColor:(UIColor*)lineColor{
  81. [self.layer setMasksToBounds:YES];
  82. [self.layer setCornerRadius:cornerRadius]; //设置矩形四个圆角半径
  83. //边框宽度
  84. [self.layer setBorderWidth:lineWidth];
  85. //设置边框颜色有两种方法:第一种如下:
  86. //CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  87. //CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 1 });
  88. //[self.layer setBorderColor:colorref];//边框颜色
  89. //第二种方法如下:
  90. self.layer.borderColor = lineColor.CGColor;
  91. }
  92. - (void)rds_addShadowAndCornerRadius:(CGFloat)cornerRadius shadowColor:(UIColor *)shadowColor {
  93. [self rds_addShadowAndCornerRadius:cornerRadius shadowOpacity:0.85f shadowRadius:1 shadowColor:shadowColor];
  94. }
  95. - (void)rds_addShadowAndCornerRadius:(CGFloat)cornerRadius shadowOpacity:(float)shadowOpacity shadowRadius:(CGFloat)shadowRadius shadowColor:(UIColor *)shadowColor {
  96. self.layer.cornerRadius = cornerRadius;
  97. UIBezierPath *roundPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
  98. cornerRadius:cornerRadius];
  99. self.layer.shadowColor = shadowColor == nil ? [UIColor blackColor].CGColor : shadowColor.CGColor;
  100. self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
  101. self.layer.shadowOpacity = shadowOpacity;
  102. self.layer.shadowRadius = shadowRadius;
  103. self.layer.shadowPath = roundPath.CGPath;
  104. }
  105. - (void)rds_addCornerRadius:(CGFloat)cornerRadius {
  106. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
  107. cornerRadius:cornerRadius];
  108. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  109. maskLayer.frame = self.bounds;
  110. maskLayer.path = maskPath.CGPath;
  111. self.layer.mask = maskLayer;
  112. self.layer.masksToBounds = YES;
  113. }
  114. @end