酷店

UIView+JZExtension.m 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. - (CGFloat)right{
  57. return CGRectGetMaxX(self.frame);
  58. }
  59. - (CGFloat)bottom{
  60. return CGRectGetMaxY(self.frame);
  61. }
  62. - (CGSize)size
  63. {
  64. return self.frame.size;
  65. }
  66. - (void)setCenterX:(CGFloat)centerX
  67. {
  68. CGPoint center = self.center;
  69. center.x = centerX;
  70. self.center = center;
  71. }
  72. - (CGFloat)centerX
  73. {
  74. return self.center.x;
  75. }
  76. - (void)setCenterY:(CGFloat)centerY
  77. {
  78. CGPoint center = self.center;
  79. center.y = centerY;
  80. self.center = center;
  81. }
  82. - (CGFloat)centerY
  83. {
  84. return self.center.y;
  85. }
  86. - (void)alignHorizontal
  87. {
  88. self.x = (self.superview.width - self.width) * 0.5;
  89. }
  90. - (void)alignVertical
  91. {
  92. self.y = (self.superview.height - self.height) *0.5;
  93. }
  94. - (void)setBorderWidth:(CGFloat)borderWidth
  95. {
  96. if (borderWidth < 0) {
  97. return;
  98. }
  99. self.layer.borderWidth = borderWidth;
  100. }
  101. - (void)setBorderColor:(UIColor *)borderColor
  102. {
  103. self.layer.borderColor = borderColor.CGColor;
  104. }
  105. - (void)setCornerRadius:(CGFloat)cornerRadius
  106. {
  107. self.layer.cornerRadius = cornerRadius;
  108. self.layer.masksToBounds = YES;
  109. }
  110. - (BOOL)isShowOnWindow
  111. {
  112. //主窗口
  113. UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
  114. //相对于父控件转换之后的rect
  115. CGRect newRect = [keyWindow convertRect:self.frame fromView:self.superview];
  116. //主窗口的bounds
  117. CGRect winBounds = keyWindow.bounds;
  118. //判断两个坐标系是否有交汇的地方,返回bool值
  119. BOOL isIntersects = CGRectIntersectsRect(newRect, winBounds);
  120. if (self.hidden != YES && self.alpha >0.01 && self.window == keyWindow && isIntersects) {
  121. return YES;
  122. }else{
  123. return NO;
  124. }
  125. }
  126. - (CGFloat)borderWidth
  127. {
  128. return self.borderWidth;
  129. }
  130. - (UIColor *)borderColor
  131. {
  132. return self.borderColor;
  133. }
  134. - (CGFloat)cornerRadius
  135. {
  136. return self.cornerRadius;
  137. }
  138. - (UIViewController *)parentController
  139. {
  140. UIResponder *responder = [self nextResponder];
  141. while (responder) {
  142. if ([responder isKindOfClass:[UIViewController class]]) {
  143. return (UIViewController *)responder;
  144. }
  145. responder = [responder nextResponder];
  146. }
  147. return nil;
  148. }
  149. - (void)addLeftCornersWithcornerRadii:(CGSize )size{
  150. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:size];
  151. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  152. maskLayer.frame = self.bounds;
  153. maskLayer.path = maskPath.CGPath;
  154. self.layer.mask = maskLayer;
  155. }
  156. - (void)addCornerRadiusByRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii{
  157. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:cornerRadii];
  158. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  159. maskLayer.frame = self.bounds;
  160. maskLayer.path = maskPath.CGPath;
  161. self.layer.mask = maskLayer;
  162. }
  163. - (void)addKeyAnimation{
  164. CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
  165. animation.duration = 0.30;
  166. animation.removedOnCompletion = YES;
  167. animation.fillMode = kCAFillModeForwards;
  168. NSMutableArray *values = [NSMutableArray array];
  169. [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
  170. [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]];
  171. [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
  172. animation.values = values;
  173. [self.layer addAnimation:animation forKey:nil];
  174. }
  175. @end