UIView+JKAdd.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //
  2. // UIView+JKAdd.m
  3. // JKBusinessBase
  4. //
  5. // Created by durgiya on 2022/3/1.
  6. //
  7. #import "UIView+JKAdd.h"
  8. #import <objc/runtime.h>
  9. #import "Masonry.h"
  10. @implementation UIView (JKAdd)
  11. #pragma mark - 分类加载方法
  12. + (void)load {
  13. static dispatch_once_t onceToken;
  14. dispatch_once(&onceToken, ^{
  15. SEL originalSelector = @selector(addSubview:);
  16. SEL swizzledSelector = @selector(jk_swizzlingAddSubview:);
  17. Method originalMethod = class_getInstanceMethod(self, originalSelector);
  18. Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
  19. BOOL didAddMethod = class_addMethod(self,
  20. originalSelector,
  21. method_getImplementation(swizzledMethod),
  22. method_getTypeEncoding(swizzledMethod));
  23. if (didAddMethod) {
  24. class_replaceMethod(self,
  25. swizzledSelector,
  26. method_getImplementation(originalMethod),
  27. method_getTypeEncoding(originalMethod));
  28. } else {
  29. method_exchangeImplementations(originalMethod, swizzledMethod);
  30. }
  31. });
  32. }
  33. #pragma mark - 视图添加边线相关方法
  34. - (void)jk_addBottomLine:(UIColor *)color {
  35. [self jk_addBottomLine:color marginLeft:0];
  36. }
  37. - (void)jk_addBottomLine:(UIColor *)color marginLeft:(CGFloat)left {
  38. UIView *line = [UIView new];
  39. line.backgroundColor = color;
  40. [self addSubview:line];
  41. [line mas_makeConstraints:^(MASConstraintMaker *make) {
  42. make.right.bottom.offset(0);
  43. make.height.offset(0.5);
  44. make.left.offset(left);
  45. }];
  46. }
  47. - (void)jk_addBottomLine:(UIColor *)color marginRight:(CGFloat)right {
  48. UIView *line = [UIView new];
  49. line.backgroundColor = color;
  50. [self addSubview:line];
  51. [line mas_makeConstraints:^(MASConstraintMaker *make) {
  52. make.left.bottom.offset(0);
  53. make.height.offset(0.5);
  54. make.right.offset(right);
  55. }];
  56. }
  57. - (void)jk_addMiddleLine:(UIColor *)color {
  58. UIView *line = [UIView new];
  59. line.backgroundColor = color;
  60. [self addSubview:line];
  61. [line mas_makeConstraints:^(MASConstraintMaker *make) {
  62. make.left.right.offset(0);
  63. make.height.offset(0.5);
  64. make.centerY.mas_equalTo(self);
  65. }];
  66. }
  67. - (void)jk_addTopLine:(UIColor *)color {
  68. UIView *line = [UIView new];
  69. line.backgroundColor = color;
  70. [self addSubview:line];
  71. [line mas_makeConstraints:^(MASConstraintMaker *make) {
  72. make.left.right.top.offset(0);
  73. make.height.offset(0.5);
  74. }];
  75. }
  76. #pragma mark - 添加阴影的相关方法
  77. - (void)jk_addShadowWithShadowColor:(UIColor *)color shadowOpacity:(CGFloat)opacity shadowRadius:(CGFloat)radius shadowOffset:(CGSize)offset shadowPathRect:(CGRect)shadowRect {
  78. //阴影颜色
  79. self.layer.shadowColor = color.CGColor;
  80. //阴影偏移
  81. self.layer.shadowOffset = offset;
  82. //阴影透明度
  83. self.layer.shadowOpacity = opacity;
  84. //阴影半径
  85. self.layer.shadowRadius = radius;
  86. if (!CGRectIsNull(shadowRect)) {
  87. //阴影路径
  88. UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowRect];
  89. self.layer.shadowPath = path.CGPath;
  90. }
  91. }
  92. #pragma mark - 坐标相关方法
  93. - (CGFloat)jk_left {
  94. return self.frame.origin.x;
  95. }
  96. - (void)setJk_left:(CGFloat)x {
  97. CGRect frame = self.frame;
  98. frame.origin.x = x;
  99. self.frame = frame;
  100. }
  101. - (CGFloat)jk_top {
  102. return self.frame.origin.y;
  103. }
  104. - (void)setJk_top:(CGFloat)y {
  105. CGRect frame = self.frame;
  106. frame.origin.y = y;
  107. self.frame = frame;
  108. }
  109. - (CGFloat)jk_right {
  110. return self.frame.origin.x + self.frame.size.width;
  111. }
  112. - (void)setJk_right:(CGFloat)right {
  113. CGRect frame = self.frame;
  114. frame.origin.x = right - frame.size.width;
  115. self.frame = frame;
  116. }
  117. - (CGFloat)jk_bottom {
  118. return self.frame.origin.y + self.frame.size.height;
  119. }
  120. - (void)setJk_bottom:(CGFloat)bottom {
  121. CGRect frame = self.frame;
  122. frame.origin.y = bottom - frame.size.height;
  123. self.frame = frame;
  124. }
  125. - (CGFloat)jk_centerX {
  126. return self.center.x;
  127. }
  128. - (void)setJk_centerX:(CGFloat)centerX {
  129. self.center = CGPointMake(centerX, self.center.y);
  130. }
  131. - (CGFloat)jk_centerY {
  132. return self.center.y;
  133. }
  134. - (void)setJk_centerY:(CGFloat)centerY {
  135. self.center = CGPointMake(self.center.x, centerY);
  136. }
  137. - (CGFloat)jk_width {
  138. return self.frame.size.width;
  139. }
  140. - (void)setJk_width:(CGFloat)width {
  141. CGRect frame = self.frame;
  142. frame.size.width = width;
  143. self.frame = frame;
  144. }
  145. - (CGFloat)jk_height {
  146. return self.frame.size.height;
  147. }
  148. - (void)setJk_height:(CGFloat)height {
  149. CGRect frame = self.frame;
  150. frame.size.height = height;
  151. self.frame = frame;
  152. }
  153. - (CGPoint)jk_origin {
  154. return self.frame.origin;
  155. }
  156. - (void)setJk_origin:(CGPoint)origin {
  157. CGRect frame = self.frame;
  158. frame.origin = origin;
  159. self.frame = frame;
  160. }
  161. - (CGSize)jk_size {
  162. return self.frame.size;
  163. }
  164. - (void)setJk_size:(CGSize)size {
  165. CGRect frame = self.frame;
  166. frame.size = size;
  167. self.frame = frame;
  168. }
  169. #pragma mark - 视图操作相关方法
  170. - (void)jk_removeAllSubviews {
  171. [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  172. }
  173. - (void)jk_setCorners:(UIRectCorner)corners
  174. withRadii:(CGSize)radii
  175. size:(CGSize)size {
  176. CGRect rect = self.bounds;
  177. rect.size = size;
  178. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect
  179. byRoundingCorners:corners
  180. cornerRadii:radii];
  181. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  182. maskLayer.frame = rect;
  183. maskLayer.path = maskPath.CGPath;
  184. self.layer.mask = maskLayer;
  185. }
  186. - (void)jk_addCornerLayer:(UIColor *)color cornerRadius:(CGFloat)radius {
  187. self.layer.masksToBounds = YES;
  188. self.layer.cornerRadius = radius;
  189. self.layer.borderColor = color.CGColor;
  190. self.layer.borderWidth = 1;
  191. }
  192. + (id)jk_loadFromXib {
  193. return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:self options:nil] lastObject];
  194. }
  195. + (id)jk_podLoadFromXibWithBundleName:(NSString *)bundleName {
  196. NSURL *bundleURL = [[NSBundle bundleForClass:self.class] URLForResource:bundleName withExtension:@"bundle"];
  197. NSBundle *bundle = [NSBundle bundleForClass:self.class];
  198. if (bundleURL) {
  199. bundle = [NSBundle bundleWithURL:bundleURL];
  200. }
  201. return [[bundle loadNibNamed:NSStringFromClass(self.class) owner:self options:nil] lastObject];
  202. }
  203. + (NSString *)jk_cellIdentifier {
  204. return NSStringFromClass(self);
  205. }
  206. - (UITableView *)jk_tableView {
  207. UIView *superView = self.superview;
  208. while (superView != nil && ![superView isKindOfClass:[UITableView class]]) {
  209. superView = superView.superview;
  210. }
  211. if (![superView isKindOfClass:UITableView.class]) {
  212. return nil;
  213. }
  214. return (UITableView *)superView;
  215. }
  216. #pragma mark - 视图层级发生变更的相关逻辑
  217. - (void)jk_swizzlingAddSubview:(UIView *)view {
  218. [self jk_swizzlingAddSubview:view];
  219. if (self.jk_didAddsubView) {
  220. self.jk_didAddsubView(view);
  221. }
  222. }
  223. - (void (^)(UIView *))jk_didAddsubView {
  224. return objc_getAssociatedObject(self, @selector(jk_didAddsubView));
  225. }
  226. - (void)setJk_didAddsubView:(void (^)(UIView *))jk_didAddsubView {
  227. objc_setAssociatedObject(self, @selector(jk_didAddsubView), jk_didAddsubView, OBJC_ASSOCIATION_COPY_NONATOMIC);
  228. }
  229. @end