UIView+Size.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // UIView+Size.m
  3. //中车运
  4. //
  5. // Created by 隋林栋 on 2017/1/13.
  6. // Copyright © 2017年 ping. All rights reserved.
  7. //
  8. #import "UIView+Size.h"
  9. //runtime
  10. #import <objc/runtime.h>
  11. static const char topToUpViewKey = '\0';
  12. static const char bottomToDownViewKey = '\0';
  13. static const char topToUpViewBGColorKey = '\0';
  14. @implementation UIView (Size)
  15. @dynamic leftCenterY;
  16. @dynamic leftTop;
  17. @dynamic leftBottom;
  18. @dynamic centerXTop;
  19. @dynamic centerXCenterY;
  20. @dynamic centerXBottom;
  21. @dynamic rightTop;
  22. @dynamic rightCenterY;
  23. @dynamic rightBottom;
  24. @dynamic widthHeight;
  25. #pragma mark 运行时
  26. -(void)setTopToUpView:(CGFloat)topToUpView
  27. {
  28. objc_setAssociatedObject(self, &topToUpViewKey, [NSNumber numberWithFloat:topToUpView], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  29. }
  30. -(CGFloat)topToUpView
  31. {
  32. NSNumber * num = objc_getAssociatedObject(self, &topToUpViewKey);
  33. if (num == nil || ![num isKindOfClass:[NSNumber class]]) {
  34. return 0;
  35. }
  36. return [num floatValue];
  37. }
  38. - (void)setBottomToDownView:(CGFloat)bottomToDownView{
  39. objc_setAssociatedObject(self, &bottomToDownViewKey, [NSNumber numberWithFloat:bottomToDownView], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  40. }
  41. - (CGFloat)bottomToDownView{
  42. NSNumber * num = objc_getAssociatedObject(self, &bottomToDownViewKey);
  43. if (num == nil || ![num isKindOfClass:[NSNumber class]]) {
  44. return 0;
  45. }
  46. return [num floatValue];
  47. }
  48. - (void)setTopToUpViewBGColor:(UIColor *)topToUpViewBGColor{
  49. objc_setAssociatedObject(self, &topToUpViewBGColorKey, topToUpViewBGColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  50. }
  51. - (UIColor *)topToUpViewBGColor{
  52. UIColor * color = objc_getAssociatedObject(self, &topToUpViewBGColorKey);
  53. return color;
  54. }
  55. //将views 组合
  56. + (instancetype)initWithViews:(NSArray *)ary{
  57. UIView * viewReturn = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
  58. viewReturn.backgroundColor = [UIColor clearColor];
  59. if (isAry(ary)) {
  60. CGFloat top = 0;
  61. for (UIView * view in ary) {
  62. if (view && [view isKindOfClass:[UIView class]]) {
  63. if (view.height) {
  64. [viewReturn addSubview:view];
  65. if (view.topToUpViewBGColor) {
  66. [viewReturn addSubview:[UIView lineWithFrame:CGRectMake(0, top, view.width, view.topToUpView) color:view.topToUpViewBGColor]];
  67. }
  68. view.top = top + view.topToUpView;
  69. top = view.bottom;
  70. }
  71. }
  72. }
  73. viewReturn.height = top;
  74. }
  75. return viewReturn;
  76. }
  77. // reset views 组合
  78. - (void)resetWithViews:(NSArray *)ary{
  79. [self removeSubViewWithTag:TAG_LINE];
  80. self.backgroundColor = COLOR_BACKGROUND;
  81. if (isAry(ary)) {
  82. CGFloat top = 0;
  83. for (UIView * view in ary) {
  84. if (!view.superview) {
  85. [self addSubview:view];
  86. }
  87. if (view && [view isKindOfClass:[UIView class]]) {
  88. if (view.height) {
  89. view.top = top + view.topToUpView;
  90. top = view.bottom;
  91. }
  92. }
  93. }
  94. self.height = top;
  95. }
  96. }
  97. //灰色背景
  98. + (instancetype)initGrayBgWithViews:(NSArray *)ary{
  99. UIView * view = [self initWithViews:ary];
  100. view.backgroundColor = COLOR_BACKGROUND;
  101. return view;
  102. }
  103. #pragma mark 组合
  104. - (void)setLeftTop:(STRUCT_XY)leftTop{
  105. self.left = leftTop.horizonX;
  106. self.top = leftTop.verticalY;
  107. }
  108. - (STRUCT_XY)leftTop{
  109. return XY(self.left, self.top);
  110. }
  111. - (void)setLeftCenterY:(STRUCT_XY)leftCenterY{
  112. self.left = leftCenterY.horizonX;
  113. self.centerY = leftCenterY.verticalY;
  114. }
  115. - (STRUCT_XY)leftCenterY{
  116. return XY(self.left, self.centerY);
  117. }
  118. - (void)setLeftBottom:(STRUCT_XY)leftBottom{
  119. self.left = leftBottom.horizonX;
  120. self.bottom = leftBottom.verticalY;
  121. }
  122. - (STRUCT_XY)leftBottom{
  123. return XY(self.left, self.bottom);
  124. }
  125. - (void)setCenterXTop:(STRUCT_XY)centerXTop{
  126. self.centerX = centerXTop.horizonX;
  127. self.top = centerXTop.verticalY;
  128. }
  129. - (STRUCT_XY)centerXTop{
  130. return XY(self.centerX, self.top);
  131. }
  132. - (void)setCenterXCenterY:(STRUCT_XY)centerXCenterY{
  133. self.centerX = centerXCenterY.horizonX;
  134. self.centerY = centerXCenterY.verticalY;
  135. }
  136. - (STRUCT_XY)centerXCenterY{
  137. return XY(self.centerX, self.centerY);
  138. }
  139. - (void)setCenterXBottom:(STRUCT_XY)centerXBottom{
  140. self.centerX = centerXBottom.horizonX;
  141. self.bottom = centerXBottom.verticalY;
  142. }
  143. - (STRUCT_XY)centerXBottom{
  144. return XY(self.centerX, self.bottom);
  145. }
  146. - (void)setRightTop:(STRUCT_XY)rightTop{
  147. self.right = rightTop.horizonX;
  148. self.top = rightTop.verticalY;
  149. }
  150. - (STRUCT_XY)rightTop{
  151. return XY(self.right, self.top);
  152. }
  153. - (void)setRightCenterY:(STRUCT_XY)rightCenterY{
  154. self.right = rightCenterY.horizonX;
  155. self.centerY = rightCenterY.verticalY;
  156. }
  157. - (STRUCT_XY)rightCenterY{
  158. return XY(self.right, self.centerY);
  159. }
  160. - (void)setRightBottom:(STRUCT_XY)rightBottom{
  161. self.right = rightBottom.horizonX;
  162. self.bottom = rightBottom.verticalY;
  163. }
  164. - (STRUCT_XY)rightBottom{
  165. return XY(self.right, self.bottom);
  166. }
  167. #pragma mark 宽高
  168. - (void)setWidthHeight:(STRUCT_XY)widthHeight{
  169. self.width = widthHeight.horizonX;
  170. self.height = widthHeight.verticalY;
  171. }
  172. - (STRUCT_XY)widthHeight{
  173. return XY(self.width, self.height);
  174. }
  175. #pragma mark 获取高度
  176. + (CGFloat)fetchHeight:(id)model{
  177. return [self fetchHeight:model className:NSStringFromClass(self)];
  178. }
  179. + (CGFloat)fetchHeight:(id)model className:(NSString *)strClassName{
  180. return [self fetchHeight:model className:isStr(strClassName)?strClassName:NSStringFromClass(self) selectorName:@"resetCellWithModel:"];
  181. }
  182. + (CGFloat)fetchHeight:(id)model selectorName:(NSString *)strSelectorName{
  183. return [self fetchHeight:model className:NSStringFromClass(self) selectorName:strSelectorName];
  184. }
  185. + (CGFloat)fetchHeight:(id)model className:(NSString *)strClassName selectorName:(NSString *)strSelectorName{
  186. static NSMutableDictionary * dic;
  187. if (dic == nil) {
  188. dic = [NSMutableDictionary dictionary];
  189. }
  190. if (!isStr(strClassName)) {
  191. strClassName = NSStringFromClass(self);
  192. }
  193. // UIView * cell = [UIView new];
  194. // if (!isDic(dic)) {
  195. UIView * cell = [NSClassFromString(strClassName) new];
  196. // [dic setObject:cell forKey:strClassName];
  197. // }else{
  198. // cell = [dic objectForKey:strClassName];
  199. // }
  200. [GlobalMethod performSelector:strSelectorName delegate:cell object:model isHasReturn:false];
  201. return cell.height;
  202. }
  203. + (CGFloat)fetchHeight:(id)model par:(id)par className:(NSString *)strClassName selectorName:(NSString *)strSelectorName{
  204. static NSMutableDictionary * dic;
  205. if (dic == nil) {
  206. dic = [NSMutableDictionary dictionary];
  207. }
  208. if (!isStr(strClassName)) {
  209. strClassName = NSStringFromClass(self);
  210. }
  211. UIView * cell = [dic objectForKey:strClassName];
  212. if (cell == nil) {
  213. cell = [NSClassFromString(strClassName) new];
  214. [dic setObject:cell forKey:strClassName];
  215. }
  216. [cell performSelector:NSSelectorFromString(strSelectorName) withObject:model withObject:par];
  217. return cell.height;
  218. }
  219. #pragma mark 判断是否显示在屏幕上
  220. - (BOOL)isShowInScreen;
  221. {
  222. CGRect screenRect = [UIScreen mainScreen].bounds;
  223. // 转换view对应window的Rect
  224. CGRect rect = [self.superview convertRect:self.frame toView:[UIApplication sharedApplication].keyWindow];
  225. if (CGRectIsEmpty(rect) || CGRectIsNull(rect)) {
  226. return false;
  227. }
  228. // 若view 隐藏
  229. if (self.hidden) {
  230. return false;
  231. }
  232. // 若没有superview
  233. if (self.superview == nil) {
  234. return false;
  235. }
  236. // 若size为CGrectZero
  237. if (CGSizeEqualToSize(rect.size, CGSizeZero)) {
  238. return false;
  239. }
  240. // 获取 该view与window 交叉的 Rect
  241. CGRect intersectionRect = CGRectIntersection(rect, screenRect);
  242. if (CGRectIsEmpty(intersectionRect) || CGRectIsNull(intersectionRect)) {
  243. return false;
  244. }
  245. return true;
  246. }
  247. - (void)addRoundCorner:(UIRectCorner)corner radius:(CGFloat )radius{
  248. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
  249. CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
  250. maskLayer.frame = self.bounds;
  251. maskLayer.path = maskPath.CGPath;
  252. self.layer.mask = maskLayer;
  253. }
  254. - (void)addRoundCorner:(UIRectCorner)corner radius:(CGFloat )radius lineWidth:(CGFloat)lineWidth lineColor:(UIColor *)lineColor{
  255. self.layer.masksToBounds = YES;
  256. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
  257. CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
  258. maskLayer.frame = self.bounds;
  259. maskLayer.path = maskPath.CGPath;
  260. self.layer.mask = maskLayer;
  261. if (lineWidth) {
  262. CAShapeLayer *borderLayer=[CAShapeLayer layer];
  263. borderLayer.path= maskPath.CGPath;
  264. borderLayer.fillColor = [UIColor clearColor].CGColor;
  265. borderLayer.strokeColor= lineColor.CGColor;
  266. borderLayer.lineWidth= lineWidth;
  267. borderLayer.frame=self.bounds;
  268. [self.layer addSublayer:borderLayer];
  269. }
  270. }
  271. - (void)removeCorner{
  272. [self.layer.mask removeFromSuperlayer];
  273. for (int i = 0; i< self.layer.sublayers.count; i++) {
  274. CALayer * layer = self.layer.sublayers[i];
  275. if ([layer isKindOfClass:CAShapeLayer.class]) {
  276. [layer removeFromSuperlayer];
  277. }
  278. }
  279. }
  280. @end