UIView+RQExtension.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 kDTActionHandlerTapBlockKey;
  11. static char kDTActionHandlerTapGestureKey;
  12. static char kDTActionHandlerLongPressBlockKey;
  13. static char kDTActionHandlerLongPressGestureKey;
  14. @implementation UIView (RQExtension)
  15. #pragma mark - Gesture
  16. - (void)setTapActionWithBlock:(void (^)(UITapGestureRecognizer*tap))block
  17. {
  18. self.userInteractionEnabled = YES;
  19. UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, &kDTActionHandlerTapGestureKey);
  20. if (!gesture)
  21. {
  22. gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(__handleActionForTapGesture:)];
  23. [self addGestureRecognizer:gesture];
  24. objc_setAssociatedObject(self, &kDTActionHandlerTapGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
  25. }
  26. objc_setAssociatedObject(self, &kDTActionHandlerTapBlockKey, block, OBJC_ASSOCIATION_COPY);
  27. }
  28. - (void)__handleActionForTapGesture:(UITapGestureRecognizer *)gesture
  29. {
  30. if (gesture.state == UIGestureRecognizerStateRecognized)
  31. {
  32. void(^action)(UITapGestureRecognizer*tap) = objc_getAssociatedObject(self, &kDTActionHandlerTapBlockKey);
  33. if (action)
  34. {
  35. action(gesture);
  36. }
  37. }
  38. }
  39. - (void)setLongPressActionWithBlock:(void (^)(UILongPressGestureRecognizer*longP))block
  40. {
  41. self.userInteractionEnabled = YES;
  42. UILongPressGestureRecognizer *gesture = objc_getAssociatedObject(self, &kDTActionHandlerLongPressGestureKey);
  43. if (!gesture)
  44. {
  45. gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(__handleActionForLongPressGesture:)];
  46. [self addGestureRecognizer:gesture];
  47. objc_setAssociatedObject(self, &kDTActionHandlerLongPressGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
  48. }
  49. objc_setAssociatedObject(self, &kDTActionHandlerLongPressBlockKey, block, OBJC_ASSOCIATION_COPY);
  50. }
  51. - (void)__handleActionForLongPressGesture:(UITapGestureRecognizer *)gesture
  52. {
  53. if (gesture.state == UIGestureRecognizerStateBegan)
  54. {
  55. void(^action)(UILongPressGestureRecognizer*longP) = objc_getAssociatedObject(self, &kDTActionHandlerLongPressBlockKey);
  56. if (action)
  57. {
  58. action((UILongPressGestureRecognizer*)gesture);
  59. }
  60. }
  61. }
  62. #pragma mark - Other
  63. /**
  64. * 判断一个控件是否真正显示在主窗口
  65. */
  66. - (BOOL)rq_isShowingOnKeyWindow
  67. {
  68. // 主窗口
  69. UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
  70. // 以主窗口左上角为坐标原点, 计算self的矩形框
  71. CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
  72. CGRect winBounds = keyWindow.bounds;
  73. // 主窗口的bounds 和 self的矩形框 是否有重叠
  74. BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
  75. return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;
  76. }
  77. /**
  78. * xib创建的view
  79. */
  80. + (instancetype)rq_viewFromXib
  81. {
  82. return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
  83. }
  84. + (instancetype)rq_viewFromXibWithFrame:(CGRect)frame {
  85. UIView *view = [self rq_viewFromXib];
  86. view.frame = frame;
  87. return view;
  88. }
  89. /**
  90. * xib中显示的属性
  91. */
  92. - (void)setBorderColor:(UIColor *)borderColor {
  93. [self.layer setBorderColor:borderColor.CGColor];
  94. }
  95. - (void)setBorderWidth:(CGFloat)borderWidth {
  96. [self.layer setBorderWidth:borderWidth];
  97. }
  98. - (void)setCornerRadius:(CGFloat)cornerRadius {
  99. [self.layer setCornerRadius:cornerRadius];
  100. }
  101. - (void)setMasksToBounds:(BOOL)masksToBounds {
  102. [self.layer setMasksToBounds:masksToBounds];
  103. }
  104. - (void)setRq_x:(CGFloat)rq_x {
  105. CGRect frame = self.frame;
  106. frame.origin.x = rq_x;
  107. self.frame = frame;
  108. }
  109. - (CGFloat)rq_x {
  110. return self.frame.origin.x;
  111. }
  112. - (void)setRq_y:(CGFloat)rq_y {
  113. CGRect frame = self.frame;
  114. frame.origin.y = rq_y;
  115. self.frame = frame;
  116. }
  117. - (CGFloat)rq_y {
  118. return self.frame.origin.y;
  119. }
  120. - (void)setRq_centerX:(CGFloat)rq_centerX {
  121. CGPoint center = self.center;
  122. center.x = rq_centerX;
  123. self.center = center;
  124. }
  125. - (CGFloat)rq_centerX {
  126. return self.center.x;
  127. }
  128. - (void)setRq_centerY:(CGFloat)rq_centerY {
  129. CGPoint center = self.center;
  130. center.y = rq_centerY;
  131. self.center = center;
  132. }
  133. - (CGFloat)rq_centerY {
  134. return self.center.y;
  135. }
  136. - (void)setRq_width:(CGFloat)rq_width {
  137. CGRect frame = self.frame;
  138. frame.size.width = rq_width;
  139. self.frame = frame;
  140. }
  141. - (CGFloat)rq_width {
  142. return self.frame.size.width;
  143. }
  144. - (void)setRq_height:(CGFloat)rq_height {
  145. CGRect frame = self.frame;
  146. frame.size.height = rq_height;
  147. self.frame = frame;
  148. }
  149. - (CGFloat)rq_height {
  150. return self.frame.size.height;
  151. }
  152. - (void)setRq_size:(CGSize)rq_size {
  153. CGRect frame = self.frame;
  154. frame.size = rq_size;
  155. self.frame = frame;
  156. }
  157. - (CGSize)rq_size {
  158. return self.frame.size;
  159. }
  160. - (void)setRq_origin:(CGPoint)rq_origin {
  161. CGRect frame = self.frame;
  162. frame.origin = rq_origin;
  163. self.frame = frame;
  164. }
  165. - (CGPoint)rq_origin {
  166. return self.frame.origin;
  167. }
  168. - (void)setRq_top:(CGFloat)rq_top {
  169. CGRect frame = self.frame;
  170. frame.origin.y = rq_top;
  171. self.frame = frame;
  172. }
  173. - (CGFloat)rq_top {
  174. return self.frame.origin.y;
  175. }
  176. - (void)setRq_left:(CGFloat)rq_left {
  177. CGRect frame = self.frame;
  178. frame.origin.x = rq_left;
  179. self.frame = frame;
  180. }
  181. - (CGFloat)rq_left{
  182. return self.frame.origin.x;
  183. }
  184. - (void)setRq_bottom:(CGFloat)rq_bottom{
  185. CGRect frame = self.frame;
  186. frame.origin.y = rq_bottom - frame.size.height;
  187. self.frame = frame;
  188. }
  189. - (CGFloat)rq_bottom {
  190. return self.frame.origin.y + self.frame.size.height;
  191. }
  192. - (void)setRq_right:(CGFloat)rq_right {
  193. CGRect frame = self.frame;
  194. frame.origin.x = rq_right - frame.size.width;
  195. self.frame = frame;
  196. }
  197. - (CGFloat)rq_right {
  198. return self.frame.origin.x + self.frame.size.width;
  199. }
  200. - (void)setX:(CGFloat)x
  201. {
  202. CGRect frame = self.frame;
  203. frame.origin.x = x;
  204. self.frame = frame;
  205. }
  206. - (CGFloat)x
  207. {
  208. return self.frame.origin.x;
  209. }
  210. - (void)setY:(CGFloat)y
  211. {
  212. CGRect frame = self.frame;
  213. frame.origin.y = y;
  214. self.frame = frame;
  215. }
  216. - (CGFloat)y
  217. {
  218. return self.frame.origin.y;
  219. }
  220. - (void)setOrigin:(CGPoint)origin
  221. {
  222. CGRect frame = self.frame;
  223. frame.origin = origin;
  224. self.frame = frame;
  225. }
  226. - (CGPoint)origin
  227. {
  228. return self.frame.origin;
  229. }
  230. - (void)setWidth:(CGFloat)width
  231. {
  232. CGRect frame = self.frame;
  233. frame.size.width = width;
  234. self.frame = frame;
  235. }
  236. - (CGFloat)width
  237. {
  238. return self.frame.size.width;
  239. }
  240. - (void)setHeight:(CGFloat)height
  241. {
  242. CGRect frame = self.frame;
  243. frame.size.height = height;
  244. self.frame = frame;
  245. }
  246. - (CGFloat)height
  247. {
  248. return self.frame.size.height;
  249. }
  250. - (void)setSize:(CGSize)size
  251. {
  252. CGRect frame = self.frame;
  253. frame.size = size;
  254. self.frame = frame;
  255. }
  256. - (CGSize)size
  257. {
  258. return self.frame.size;
  259. }
  260. - (void)setBottom:(CGFloat)bottom
  261. {
  262. CGRect frame = self.frame;
  263. frame.origin.y = bottom - frame.size.height;
  264. self.frame = frame;
  265. }
  266. - (CGFloat)bottom
  267. {
  268. return CGRectGetMaxY(self.frame);
  269. }
  270. - (CGFloat)tail
  271. {
  272. return CGRectGetMaxX(self.frame);
  273. }
  274. - (void)setTail:(CGFloat)tail
  275. {
  276. CGRect frame = self.frame;
  277. frame.origin.x = tail - frame.size.width;
  278. self.frame = frame;
  279. }
  280. - (void)setMiddleX:(CGFloat)middleX
  281. {
  282. CGRect frame = self.frame;
  283. frame.origin.x = middleX - frame.size.width / 2;
  284. self.frame = frame;
  285. }
  286. - (CGFloat)middleX
  287. {
  288. return CGRectGetMidX(self.frame);
  289. }
  290. - (void)setMiddleY:(CGFloat)middleY
  291. {
  292. CGRect frame = self.frame;
  293. frame.origin.y = middleY - frame.size.height / 2 ;
  294. self.frame = frame;
  295. }
  296. - (CGFloat)middleY
  297. {
  298. return CGRectGetMidY(self.frame);
  299. }
  300. + (void)removeSubviewsRecursively:(UIView *)view {
  301. for (UIView *subview in view.subviews) {
  302. [UIView removeSubviewsRecursively:subview];
  303. }
  304. [view removeFromSuperview];
  305. }
  306. @end