No Description

UIView+JZExtension.m 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // UIView+LBExtension.m
  3. // XianYu
  4. //
  5. // Created by li bo on 16/5/28.
  6. // Copyright © 2016年 li bo. All rights reserved.
  7. //
  8. #import "UIView+JZExtension.h"
  9. @implementation UIView (JZExtension)
  10. - (void)setX:(CGFloat)x
  11. {
  12. CGRect frame = self.frame;
  13. frame.origin.x = x;
  14. self.frame = frame;
  15. }
  16. - (CGFloat)x
  17. {
  18. return self.frame.origin.x;
  19. }
  20. - (void)setY:(CGFloat)y
  21. {
  22. CGRect frame = self.frame;
  23. frame.origin.y = y;
  24. self.frame = frame;
  25. }
  26. - (CGFloat)y
  27. {
  28. return self.frame.origin.y;
  29. }
  30. - (void)setWidth:(CGFloat)width
  31. {
  32. CGRect frame = self.frame;
  33. frame.size.width = width;
  34. self.frame = frame;
  35. }
  36. - (CGFloat)width
  37. {
  38. return self.frame.size.width;
  39. }
  40. - (void)setHeight:(CGFloat)height
  41. {
  42. CGRect frame = self.frame;
  43. frame.size.height = height;
  44. self.frame= frame;
  45. }
  46. - (CGFloat)height
  47. {
  48. return self.frame.size.height;
  49. }
  50. - (void)setSize:(CGSize)size
  51. {
  52. CGRect frame = self.frame;
  53. frame.size = size;
  54. self.frame = frame;
  55. }
  56. - (CGSize)size
  57. {
  58. return self.frame.size;
  59. }
  60. - (void)setCenterX:(CGFloat)centerX
  61. {
  62. CGPoint center = self.center;
  63. center.x = centerX;
  64. self.center = center;
  65. }
  66. - (CGFloat)centerX
  67. {
  68. return self.center.x;
  69. }
  70. - (void)setCenterY:(CGFloat)centerY
  71. {
  72. CGPoint center = self.center;
  73. center.y = centerY;
  74. self.center = center;
  75. }
  76. - (CGFloat)centerY
  77. {
  78. return self.center.y;
  79. }
  80. - (void)alignHorizontal
  81. {
  82. self.x = (self.superview.width - self.width) * 0.5;
  83. }
  84. - (void)alignVertical
  85. {
  86. self.y = (self.superview.height - self.height) *0.5;
  87. }
  88. - (void)setBorderWidth:(CGFloat)borderWidth
  89. {
  90. if (borderWidth < 0) {
  91. return;
  92. }
  93. self.layer.borderWidth = borderWidth;
  94. }
  95. - (void)setBorderColor:(UIColor *)borderColor
  96. {
  97. self.layer.borderColor = borderColor.CGColor;
  98. }
  99. - (void)setCornerRadius:(CGFloat)cornerRadius
  100. {
  101. self.layer.cornerRadius = cornerRadius;
  102. self.layer.masksToBounds = YES;
  103. }
  104. - (BOOL)isShowOnWindow
  105. {
  106. //主窗口
  107. UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
  108. //相对于父控件转换之后的rect
  109. CGRect newRect = [keyWindow convertRect:self.frame fromView:self.superview];
  110. //主窗口的bounds
  111. CGRect winBounds = keyWindow.bounds;
  112. //判断两个坐标系是否有交汇的地方,返回bool值
  113. BOOL isIntersects = CGRectIntersectsRect(newRect, winBounds);
  114. if (self.hidden != YES && self.alpha >0.01 && self.window == keyWindow && isIntersects) {
  115. return YES;
  116. }else{
  117. return NO;
  118. }
  119. }
  120. - (CGFloat)borderWidth
  121. {
  122. return self.borderWidth;
  123. }
  124. - (UIColor *)borderColor
  125. {
  126. return self.borderColor;
  127. }
  128. - (CGFloat)cornerRadius
  129. {
  130. return self.cornerRadius;
  131. }
  132. - (UIViewController *)parentController
  133. {
  134. UIResponder *responder = [self nextResponder];
  135. while (responder) {
  136. if ([responder isKindOfClass:[UIViewController class]]) {
  137. return (UIViewController *)responder;
  138. }
  139. responder = [responder nextResponder];
  140. }
  141. return nil;
  142. }
  143. - (void)addLeftCornersWithcornerRadii:(CGSize )size{
  144. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:size];
  145. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  146. maskLayer.frame = self.bounds;
  147. maskLayer.path = maskPath.CGPath;
  148. self.layer.mask = maskLayer;
  149. }
  150. - (void)addCornerRadiusByRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii{
  151. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:cornerRadii];
  152. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  153. maskLayer.frame = self.bounds;
  154. maskLayer.path = maskPath.CGPath;
  155. self.layer.mask = maskLayer;
  156. }
  157. @end