UIView+Category.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // UIView+Category.m
  3. // 乐销
  4. //
  5. // Created by 刘惠萍 on 2017/5/29.
  6. // Copyright © 2017年 ping. All rights reserved.
  7. //
  8. #import "UIView+Category.h"
  9. @implementation UIView (Category)
  10. //get
  11. - (BOOL)isEdited{
  12. if ([self isKindOfClass:[UITextField class]]||[self isKindOfClass:[UITextView class]]) {
  13. UITextView * text = (UITextView *)self;
  14. if (self.userInteractionEnabled && isStr(text.text)) {
  15. if ([text.text intValue] != 1) {
  16. return true;
  17. }
  18. }
  19. }
  20. for (UIView * viewSub in self.subviews) {
  21. if (viewSub.isEdited) {
  22. return true;
  23. }
  24. }
  25. return false;
  26. }
  27. //fist responder
  28. - (UIView *)fetchFirstResponder
  29. {
  30. if (self.isFirstResponder) {
  31. return self;
  32. }
  33. for (UIView *subView in self.subviews) {
  34. UIView *firstResponder = [subView fetchFirstResponder];
  35. if (firstResponder != nil) {
  36. return firstResponder;
  37. }
  38. }
  39. return nil;
  40. }
  41. //获取所在vc
  42. - (UIViewController *)fetchVC{
  43. UIView * view = self;
  44. while (view) {
  45. UIResponder* nextResponder = [view nextResponder];
  46. if (nextResponder && [nextResponder isKindOfClass:[UIViewController class]]) {
  47. return (UIViewController*)nextResponder;
  48. }
  49. view = view.superview;
  50. }
  51. return nil;
  52. }
  53. //移除全部
  54. - (void)removeAllSubViews{
  55. while (self.subviews.count) {
  56. [self.subviews.lastObject removeFromSuperview];
  57. }
  58. }
  59. //增加顶部高度
  60. - (void)addSubViewsTopHeight:(CGFloat)height{
  61. for (UIView * view in self.subviews) {
  62. view.top += height;
  63. }
  64. self.height += height;
  65. }
  66. // 移除视图
  67. - (void)removeSubViewWithTag:(NSInteger)tag{
  68. if (self == nil) return;
  69. NSArray * aryView = self.subviews;
  70. for (UIView * viewSub in aryView) {
  71. if (viewSub.tag == tag) {
  72. [viewSub removeFromSuperview];
  73. }
  74. }
  75. }
  76. //获取子视图 根据tag
  77. - (NSMutableArray *)fetchSubViewsWithTag:(NSInteger)tag{
  78. NSMutableArray * aryReturn = [NSMutableArray array];
  79. for (UIView * subView in self.subviews) {
  80. if (subView.tag == tag) {
  81. [aryReturn addObject:subView];
  82. }
  83. }
  84. return aryReturn;
  85. }
  86. //添加线
  87. - (CGFloat)addLineFrame:(CGRect)rect{
  88. return [self addLineFrame:rect color:COLOR_LINE];
  89. }
  90. - (CGFloat)addLineFrame:(CGRect)rect tag:(NSInteger)tag{
  91. return [self addLineFrame:rect color:COLOR_LINE tag:tag];
  92. }
  93. - (CGFloat)addLineFrame:(CGRect)rect color:(UIColor *)color{
  94. return [self addLineFrame:rect color:color tag:TAG_LINE];
  95. }
  96. - (CGFloat)addLineFrame:(CGRect)rect color:(UIColor *)color tag:(NSInteger)tag{
  97. UIView * viewLine = [UIView lineWithFrame:rect color:color];
  98. viewLine.tag = tag;
  99. [self addSubview:viewLine];
  100. return viewLine.bottom;
  101. }
  102. - (CGFloat)addLineWithHeight:(CGFloat)height{
  103. return [self addLineFrame:CGRectMake(0, 0, self.width, height) color:COLOR_LINE];
  104. }
  105. //获取线视图
  106. + (UIView *)lineWithFrame:(CGRect)rect color:(UIColor *)color{
  107. UIView * viewLine = [[UIView alloc]initWithFrame:rect];
  108. viewLine.backgroundColor = color;
  109. viewLine.tag = TAG_LINE;
  110. return viewLine;
  111. }
  112. + (UIView *)lineWithHeight:(CGFloat)height{
  113. return [self lineWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height) color:COLOR_LINE];
  114. }
  115. + (UIView *)lineWithHeight:(CGFloat)height backGroundColor:(UIColor *)color{
  116. return [self lineWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height) color:color];
  117. }
  118. /**
  119. 增加点击事件
  120. @param target 目标
  121. @param action 点击事件
  122. */
  123. - (void)addTarget:(id)target action:(SEL)action{
  124. if(target){
  125. self.userInteractionEnabled = YES;
  126. UITapGestureRecognizer *tapClick=[[UITapGestureRecognizer alloc]initWithTarget:target action:action];
  127. tapClick.delegate = target;
  128. [self addGestureRecognizer:tapClick];
  129. }
  130. }
  131. - (void)addKeyboardHideGesture{
  132. UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureHideKeyboardClick)];
  133. tap.cancelsTouchesInView = NO;
  134. tap.delegate = self;
  135. [self addGestureRecognizer:tap];
  136. }
  137. - (void)tapGestureHideKeyboardClick{
  138. [GlobalMethod hideKeyboard];
  139. }
  140. #pragma mark gesture delegate
  141. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
  142. if ([touch.view isKindOfClass:[UIControl class]]) {
  143. return false;
  144. }if ([touch.view isKindOfClass:[UICollectionView class]]){
  145. return false;
  146. }
  147. return true;
  148. }
  149. - (void)addShadowToView:(UIView *)theView withColor:(UIColor *)theColor {
  150. theView.layer.cornerRadius = 6;
  151. // 阴影颜色
  152. theView.layer.shadowColor = theColor.CGColor;
  153. // 阴影偏移,默认(0, -3)
  154. theView.layer.shadowOffset = CGSizeMake(0,1);
  155. // 阴影透明度,默认0
  156. theView.layer.shadowOpacity = 1;
  157. // 阴影半径,默认3
  158. theView.layer.shadowRadius = 10;
  159. }
  160. @end