BezierPathTool.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // BezierPathTool.m
  3. // 圆角设置测试
  4. //
  5. // Created by Zuobian on 2019/12/4.
  6. // Copyright © 2019 admin. All rights reserved.
  7. //
  8. #import "BezierPathTool.h"
  9. @implementation BezierPathTool
  10. - (instancetype)initWithRadius:(float)radius rectSize:(CGSize)rectSize fillColor:(UIColor *)fillColor {
  11. if (self = [super init]) {
  12. _radius = radius;
  13. _rectSize = rectSize;
  14. _fillColor = fillColor;
  15. [self configData];
  16. }
  17. return self;
  18. }
  19. - (instancetype)initWithRadius_TopLeft:(float)radius_TL
  20. radius_TopRight:(float)radius_TR
  21. radius_BottomLeft:(float)radius_BL
  22. radius_BottomRight:(float)radius_BR
  23. rectSize:(CGSize)rectSize
  24. fillColor:(UIColor *)fillColor {
  25. if (self = [super init]) {
  26. _radius_TopLeft = radius_TL;
  27. _radius_TopRight = radius_TR;
  28. _radius_BottomLeft = radius_BL;
  29. _radius_BottomRight = radius_BR;
  30. _rectSize = rectSize;
  31. _fillColor = fillColor;
  32. [self configCornerPointData];
  33. }
  34. return self;
  35. }
  36. - (void)configData {
  37. _hLeftUpPoint = CGPointMake(0, 0);
  38. _hRightUpPoint = CGPointMake(_rectSize.width, 0);
  39. _hLeftDownPoint = CGPointMake(0, _rectSize.height);
  40. _vLeftUpPoint = CGPointMake(0, 0);
  41. _vRightDownPoint = CGPointMake(_rectSize.width, _rectSize.height);
  42. _centerLeftUp = CGPointMake(0, 0);
  43. _centerRightUp = CGPointMake(_rectSize.width, 0);
  44. _centerLeftDown = CGPointMake(0, _rectSize.height);
  45. _centerRightDown = CGPointMake(_rectSize.width, _rectSize.height);
  46. }
  47. - (void)configCornerPointData {
  48. [self configTopLeftPoint];
  49. [self configTopRightPoint];
  50. [self configBottomLeftPoint];
  51. [self configBottomRightPoint];
  52. }
  53. - (void)configAllCornerPoint {
  54. _hLeftUpPoint = CGPointMake(_radius, 0);//(10,0)
  55. _hRightUpPoint = CGPointMake(_rectSize.width - _radius, 0);//(30,0)
  56. _hLeftDownPoint = CGPointMake(_radius, _rectSize.height);//(10,40)
  57. _vLeftUpPoint = CGPointMake(0, _radius);//(0,10)
  58. _vRightDownPoint = CGPointMake(_rectSize.width, _rectSize.height - _radius);//(40,30)
  59. _centerLeftUp = CGPointMake(_radius, _radius);//(10,10)
  60. _centerRightUp = CGPointMake(_rectSize.width - _radius, _radius);//(30,10)
  61. _centerLeftDown = CGPointMake(_radius, _rectSize.height - _radius);//(10,30)
  62. _centerRightDown = CGPointMake(_rectSize.width - _radius, _rectSize.height - _radius);//(30,30)
  63. }
  64. - (void)configTopLeftPoint {
  65. float radius = _radius_TopLeft ?: _radius;
  66. _hLeftUpPoint = CGPointMake(radius, 0);//(10,0)
  67. _vLeftUpPoint = CGPointMake(0, radius);//(0,10)
  68. _centerLeftUp = CGPointMake(radius, radius);//(10,10)
  69. }
  70. - (void)configTopRightPoint {
  71. float radius = _radius_TopRight ?: _radius;
  72. _hRightUpPoint = CGPointMake(_rectSize.width - radius, 0);//(30,0)
  73. _centerRightUp = CGPointMake(_rectSize.width - radius, radius);//(30,10)
  74. }
  75. - (void)configBottomLeftPoint {
  76. float radius = _radius_BottomLeft ?: _radius;
  77. _hLeftDownPoint = CGPointMake(radius, _rectSize.height);//(10,40)
  78. _centerLeftDown = CGPointMake(radius, _rectSize.height - radius);//(10,30)
  79. }
  80. - (void)configBottomRightPoint {
  81. float radius = _radius_BottomRight ?: _radius;
  82. _vRightDownPoint = CGPointMake(_rectSize.width, _rectSize.height - radius);//(40,30)
  83. _centerRightDown = CGPointMake(_rectSize.width - radius, _rectSize.height - radius);//(30,30)
  84. }
  85. // 绘制贝塞尔曲线
  86. - (UIBezierPath *)configCornerBezierPath:(UIBezierPath *)bezierPath {
  87. [bezierPath moveToPoint:self.hLeftUpPoint];
  88. [bezierPath addLineToPoint:self.hRightUpPoint];
  89. [bezierPath addArcWithCenter:self.centerRightUp radius:self.radius_TopRight ?: self.radius startAngle:(CGFloat)(M_PI * 3 / 2) endAngle:(CGFloat)(M_PI * 2) clockwise: true];
  90. [bezierPath addLineToPoint:self.vRightDownPoint];
  91. [bezierPath addArcWithCenter:self.centerRightDown radius:self.radius_BottomRight ?: self.radius startAngle: 0 endAngle: (CGFloat)(M_PI / 2) clockwise: true];
  92. [bezierPath addLineToPoint:self.hLeftDownPoint];
  93. [bezierPath addArcWithCenter:self.centerLeftDown radius:self.radius_BottomLeft ?: self.radius startAngle: (CGFloat)(M_PI / 2) endAngle: (CGFloat)(M_PI) clockwise: true];
  94. [bezierPath addLineToPoint:self.vLeftUpPoint];
  95. [bezierPath addArcWithCenter:self.centerLeftUp radius:self.radius_TopLeft ?: self.radius startAngle: (CGFloat)(M_PI) endAngle: (CGFloat)(M_PI * 3 / 2) clockwise: true];
  96. [bezierPath addLineToPoint:self.hLeftUpPoint];
  97. [bezierPath closePath];
  98. [bezierPath moveToPoint:CGPointZero];
  99. [bezierPath addLineToPoint:CGPointMake(0, self.rectSize.height)];
  100. [bezierPath addLineToPoint:CGPointMake(self.rectSize.width, self.rectSize.height)];
  101. [bezierPath addLineToPoint:CGPointMake(self.rectSize.width, 0)];
  102. [bezierPath addLineToPoint:CGPointZero];
  103. [bezierPath closePath];
  104. [self.fillColor setFill];
  105. [bezierPath fill];
  106. return bezierPath;
  107. }
  108. @end