UIView+RQExtension.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // UIView+RQExtension.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/16.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "UIView+RQExtension.h"
  9. #import <objc/runtime.h>
  10. static char kActionHandlerTapBlockKey;
  11. static char kActionHandlerTapGestureKey;
  12. static char kActionHandlerLongPressBlockKey;
  13. static char kActionHandlerLongPressGestureKey;
  14. @implementation UIView (RQExtension)
  15. /**
  16. * 判断一个控件是否真正显示在主窗口
  17. */
  18. - (BOOL)rq_isShowingOnKeyWindow
  19. {
  20. // 主窗口
  21. UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
  22. // 以主窗口左上角为坐标原点, 计算self的矩形框
  23. CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
  24. CGRect winBounds = keyWindow.bounds;
  25. // 主窗口的bounds 和 self的矩形框 是否有重叠
  26. BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
  27. return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;
  28. }
  29. /**
  30. * xib创建的view
  31. */
  32. + (instancetype)rq_viewFromXib
  33. {
  34. return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
  35. }
  36. + (instancetype)rq_viewFromXibWithFrame:(CGRect)frame {
  37. UIView *view = [self rq_viewFromXib];
  38. view.frame = frame;
  39. return view;
  40. }
  41. - (void)setRq_x:(CGFloat)rq_x {
  42. CGRect frame = self.frame;
  43. frame.origin.x = rq_x;
  44. self.frame = frame;
  45. }
  46. - (CGFloat)rq_x {
  47. return self.frame.origin.x;
  48. }
  49. - (void)setRq_y:(CGFloat)rq_y {
  50. CGRect frame = self.frame;
  51. frame.origin.y = rq_y;
  52. self.frame = frame;
  53. }
  54. - (CGFloat)rq_y {
  55. return self.frame.origin.y;
  56. }
  57. - (void)setRq_centerX:(CGFloat)rq_centerX {
  58. CGPoint center = self.center;
  59. center.x = rq_centerX;
  60. self.center = center;
  61. }
  62. - (CGFloat)rq_centerX {
  63. return self.center.x;
  64. }
  65. - (void)setRq_centerY:(CGFloat)rq_centerY {
  66. CGPoint center = self.center;
  67. center.y = rq_centerY;
  68. self.center = center;
  69. }
  70. - (CGFloat)rq_centerY {
  71. return self.center.y;
  72. }
  73. - (void)setRq_width:(CGFloat)rq_width {
  74. CGRect frame = self.frame;
  75. frame.size.width = rq_width;
  76. self.frame = frame;
  77. }
  78. - (CGFloat)rq_width {
  79. return self.frame.size.width;
  80. }
  81. - (void)setRq_height:(CGFloat)rq_height {
  82. CGRect frame = self.frame;
  83. frame.size.height = rq_height;
  84. self.frame = frame;
  85. }
  86. - (CGFloat)rq_height {
  87. return self.frame.size.height;
  88. }
  89. - (void)setRq_size:(CGSize)rq_size {
  90. CGRect frame = self.frame;
  91. frame.size = rq_size;
  92. self.frame = frame;
  93. }
  94. - (CGSize)rq_size {
  95. return self.frame.size;
  96. }
  97. - (void)setRq_origin:(CGPoint)rq_origin {
  98. CGRect frame = self.frame;
  99. frame.origin = rq_origin;
  100. self.frame = frame;
  101. }
  102. - (CGPoint)rq_origin {
  103. return self.frame.origin;
  104. }
  105. - (void)setRq_top:(CGFloat)rq_top {
  106. CGRect frame = self.frame;
  107. frame.origin.y = rq_top;
  108. self.frame = frame;
  109. }
  110. - (CGFloat)rq_top {
  111. return self.frame.origin.y;
  112. }
  113. - (void)setRq_left:(CGFloat)rq_left {
  114. CGRect frame = self.frame;
  115. frame.origin.x = rq_left;
  116. self.frame = frame;
  117. }
  118. - (CGFloat)rq_left{
  119. return self.frame.origin.x;
  120. }
  121. - (void)setRq_bottom:(CGFloat)rq_bottom{
  122. CGRect frame = self.frame;
  123. frame.origin.y = rq_bottom - frame.size.height;
  124. self.frame = frame;
  125. }
  126. - (CGFloat)rq_bottom {
  127. return self.frame.origin.y + self.frame.size.height;
  128. }
  129. - (void)setRq_right:(CGFloat)rq_right {
  130. CGRect frame = self.frame;
  131. frame.origin.x = rq_right - frame.size.width;
  132. self.frame = frame;
  133. }
  134. - (CGFloat)rq_right {
  135. return self.frame.origin.x + self.frame.size.width;
  136. }
  137. - (void)addTapActionWithBlock:(GestureActionBlock)block
  138. {
  139. UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, &kActionHandlerTapGestureKey);
  140. if (!gesture)
  141. {
  142. gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForTapGesture:)];
  143. [self addGestureRecognizer:gesture];
  144. objc_setAssociatedObject(self, &kActionHandlerTapGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
  145. }
  146. objc_setAssociatedObject(self, &kActionHandlerTapBlockKey, block, OBJC_ASSOCIATION_COPY);
  147. }
  148. - (void)handleActionForTapGesture:(UITapGestureRecognizer*)gesture
  149. {
  150. if (gesture.state == UIGestureRecognizerStateRecognized)
  151. {
  152. GestureActionBlock block = objc_getAssociatedObject(self, &kActionHandlerTapBlockKey);
  153. if (block)
  154. {
  155. block(gesture);
  156. }
  157. }
  158. }
  159. - (void)addLongPressActionWithBlock:(GestureActionBlock)block
  160. {
  161. UILongPressGestureRecognizer *gesture = objc_getAssociatedObject(self, &kActionHandlerLongPressGestureKey);
  162. if (!gesture)
  163. {
  164. gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForLongPressGesture:)];
  165. [self addGestureRecognizer:gesture];
  166. objc_setAssociatedObject(self, &kActionHandlerLongPressGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
  167. }
  168. objc_setAssociatedObject(self, &kActionHandlerLongPressBlockKey, block, OBJC_ASSOCIATION_COPY);
  169. }
  170. - (void)handleActionForLongPressGesture:(UITapGestureRecognizer*)gesture
  171. {
  172. if (gesture.state == UIGestureRecognizerStateRecognized)
  173. {
  174. GestureActionBlock block = objc_getAssociatedObject(self, &kActionHandlerLongPressBlockKey);
  175. if (block)
  176. {
  177. block(gesture);
  178. }
  179. }
  180. }
  181. @end