APRoundedButton.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // APRoundedButton.m
  3. // jiaPei
  4. //
  5. // Created by EchoShacolee on 2018/6/14.
  6. // Copyright © 2018年 JCZ. All rights reserved.
  7. //
  8. #import "APRoundedButton.h"
  9. @interface APRoundedButton()
  10. {
  11. UIBezierPath *maskPath;
  12. }
  13. @end
  14. @implementation APRoundedButton
  15. - (void)makeCorner {
  16. UIRectCorner corners;
  17. switch ( self.style )
  18. {
  19. case 0:
  20. corners = UIRectCornerBottomLeft;
  21. break;
  22. case 1:
  23. corners = UIRectCornerBottomRight;
  24. break;
  25. case 2:
  26. corners = UIRectCornerTopLeft;
  27. break;
  28. case 3:
  29. corners = UIRectCornerTopRight;
  30. break;
  31. case 4:
  32. corners = UIRectCornerBottomLeft | UIRectCornerBottomRight;
  33. break;
  34. case 5:
  35. corners = UIRectCornerTopLeft | UIRectCornerTopRight;
  36. break;
  37. case 6:
  38. corners = UIRectCornerBottomLeft | UIRectCornerTopLeft;
  39. break;
  40. case 7:
  41. corners = UIRectCornerBottomRight | UIRectCornerTopRight;
  42. break;
  43. case 8:
  44. corners = UIRectCornerBottomRight | UIRectCornerTopRight | UIRectCornerTopLeft;
  45. break;
  46. case 9:
  47. corners = UIRectCornerBottomRight | UIRectCornerTopRight | UIRectCornerBottomLeft;
  48. break;
  49. case 10:
  50. corners = -1;
  51. break;
  52. case 11:
  53. corners = -2;
  54. break;
  55. default:
  56. corners = UIRectCornerAllCorners;
  57. break;
  58. }
  59. if (corners == -1) {
  60. maskPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.height) radius:self.bounds.size.height-_nj_cornerRaduous startAngle:0 endAngle:M_PI clockwise:NO];
  61. self.titleLabel.y += _nj_cornerRaduous;
  62. }else if (corners == -2) {
  63. maskPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2, 0) radius:self.bounds.size.height-_nj_cornerRaduous startAngle:M_PI endAngle:2*M_PI clockwise:NO];
  64. self.titleLabel.y -= _nj_cornerRaduous;
  65. }else{
  66. _nj_cornerRaduous = _nj_cornerRaduous ?: 10.0;
  67. maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
  68. byRoundingCorners:corners
  69. cornerRadii:CGSizeMake(_nj_cornerRaduous, _nj_cornerRaduous)];
  70. }
  71. CAShapeLayer *maskLayer = [CAShapeLayer layer];
  72. maskLayer.frame = self.bounds;
  73. maskLayer.path = maskPath.CGPath;
  74. self.layer.mask = maskLayer;
  75. [self.layer setMasksToBounds:YES];
  76. }
  77. - (instancetype)initWithFrame:(CGRect)frame
  78. {
  79. if (self = [super initWithFrame:frame]) {
  80. [self setupUIOnce];
  81. }
  82. return self;
  83. }
  84. - (instancetype)initWithCoder:(NSCoder *)coder
  85. {
  86. self = [super initWithCoder:coder];
  87. if (self) {
  88. [self setupUIOnce];
  89. }
  90. return self;
  91. }
  92. - (void)setupUIOnce
  93. {
  94. [self makeCorner];
  95. }
  96. - (void)layoutSubviews {
  97. [super layoutSubviews];
  98. [self makeCorner];
  99. }
  100. - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
  101. BOOL res = [super pointInside:point withEvent:event];
  102. if (res) {
  103. if ([maskPath containsPoint:point]) {
  104. return YES;
  105. }
  106. }
  107. return NO;
  108. }
  109. @end