UIView+MAC.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. //
  2. // UIView+MAC.m
  3. // WeiSchoolTeacher
  4. //
  5. // Created by MacKun on 15/12/11.
  6. // Copyright © 2015年 MacKun. All rights reserved.
  7. //
  8. #import "UIView+MAC.h"
  9. #import <QuartzCore/QuartzCore.h>
  10. #import <objc/runtime.h>
  11. static char BAViewCookieKey;
  12. @implementation UIView(MAC)
  13. /**
  14. * @brief 找到当前view所在的viewcontroler
  15. */
  16. - (UIViewController *)viewController
  17. {
  18. UIResponder *responder = self.nextResponder;
  19. do {
  20. if ([responder isKindOfClass:[UIViewController class]]) {
  21. return (UIViewController *)responder;
  22. }
  23. responder = responder.nextResponder;
  24. } while (responder);
  25. return nil;
  26. }
  27. - cookie
  28. {
  29. return objc_getAssociatedObject(self, &BAViewCookieKey);
  30. }
  31. - (void)setCookie:cookie
  32. {
  33. objc_setAssociatedObject(self, &BAViewCookieKey, cookie, OBJC_ASSOCIATION_RETAIN);
  34. }
  35. - (void)setSize:(CGSize)size;
  36. {
  37. CGPoint origin = [self frame].origin;
  38. [self setFrame:CGRectMake(origin.x, origin.y, size.width, size.height)];
  39. }
  40. - (CGSize)size;
  41. {
  42. return [self frame].size;
  43. }
  44. - (CGFloat)left;
  45. {
  46. return CGRectGetMinX([self frame]);
  47. }
  48. - (void)setLeft:(CGFloat)x;
  49. {
  50. CGRect frame = [self frame];
  51. frame.origin.x = x;
  52. [self setFrame:frame];
  53. }
  54. - (CGFloat)top;
  55. {
  56. return CGRectGetMinY([self frame]);
  57. }
  58. - (void)setTop:(CGFloat)y;
  59. {
  60. CGRect frame = [self frame];
  61. frame.origin.y = y;
  62. [self setFrame:frame];
  63. }
  64. - (CGFloat)right;
  65. {
  66. return CGRectGetMaxX([self frame]);
  67. }
  68. - (void)setRight:(CGFloat)right;
  69. {
  70. CGRect frame = [self frame];
  71. frame.origin.x = right - frame.size.width;
  72. [self setFrame:frame];
  73. }
  74. - (CGFloat)bottom;
  75. {
  76. return CGRectGetMaxY([self frame]);
  77. }
  78. - (void)setBottom:(CGFloat)bottom;
  79. {
  80. CGRect frame = [self frame];
  81. frame.origin.y = bottom - frame.size.height;
  82. [self setFrame:frame];
  83. }
  84. - (CGFloat)centerX;
  85. {
  86. return [self center].x;
  87. }
  88. - (void)setCenterX:(CGFloat)centerX;
  89. {
  90. [self setCenter:CGPointMake(centerX, self.center.y)];
  91. }
  92. - (CGFloat)centerY;
  93. {
  94. return [self center].y;
  95. }
  96. - (void)setCenterY:(CGFloat)centerY;
  97. {
  98. [self setCenter:CGPointMake(self.center.x, centerY)];
  99. }
  100. - (CGFloat)width;
  101. {
  102. return CGRectGetWidth([self frame]);
  103. }
  104. - (void)setWidth:(CGFloat)width;
  105. {
  106. CGRect frame = [self frame];
  107. frame.size.width = width;
  108. [self setFrame:CGRectStandardize(frame)];
  109. }
  110. - (CGFloat)height;
  111. {
  112. return CGRectGetHeight([self frame]);
  113. }
  114. - (void)setHeight:(CGFloat)height;
  115. {
  116. CGRect frame = [self frame];
  117. frame.size.height = height;
  118. [self setFrame:CGRectStandardize(frame)];
  119. }
  120. -(void)addShadowonBottom
  121. {
  122. self.layer.shadowOpacity = 0.4;
  123. self.layer.shadowRadius = 0.7;
  124. self.layer.shadowOffset = CGSizeMake(0, 0);
  125. UIBezierPath *path = [UIBezierPath bezierPath];
  126. CGPoint p1 = CGPointMake(0.0, 0.0+self.frame.size.height);
  127. CGPoint p2 = CGPointMake(0.0+self.frame.size.width, p1.y);
  128. CGPoint c1 = CGPointMake((p1.x+p2.x)/256 , p1.y+1.50);
  129. CGPoint c2 = CGPointMake(c1.x*255, c1.y);
  130. [path moveToPoint:p1];
  131. [path addCurveToPoint:p2 controlPoint1:c1 controlPoint2:c2];
  132. self.layer.shadowPath = path.CGPath;
  133. }
  134. -(void)addShadowonTop
  135. {
  136. self.layer.shadowOpacity = 0.4;
  137. self.layer.shadowRadius = 0.7;
  138. self.layer.shadowOffset = CGSizeMake(0, 0);
  139. UIBezierPath *path = [UIBezierPath bezierPath];
  140. CGPoint p1 = CGPointMake(0.0, 0.0);
  141. CGPoint p2 = CGPointMake(0.0+self.frame.size.width, p1.y);
  142. CGPoint c1 = CGPointMake((p1.x+p2.x)/4 , p1.y-2.5);
  143. CGPoint c2 = CGPointMake(c1.x*3, c1.y);
  144. [path moveToPoint:p1];
  145. [path addCurveToPoint:p2 controlPoint1:c1 controlPoint2:c2];
  146. self.layer.shadowPath = path.CGPath;
  147. }
  148. -(void)addGrayGradientShadow
  149. {
  150. self.layer.shadowOpacity = 0.4;
  151. CGFloat rectWidth = 10.0;
  152. CGFloat rectHeight = self.frame.size.height;
  153. CGMutablePathRef shadowPath = CGPathCreateMutable();
  154. CGPathMoveToPoint(shadowPath, NULL, 0.0, 0.0);
  155. CGPathAddRect(shadowPath, NULL, CGRectMake(0.0-rectWidth, 0.0, rectWidth, rectHeight));
  156. CGPathAddRect(shadowPath, NULL, CGRectMake(self.frame.size.width, 0.0, rectWidth, rectHeight));
  157. self.layer.shadowPath = shadowPath;
  158. CGPathRelease(shadowPath);
  159. self.layer.shadowOffset = CGSizeMake(0, 0);
  160. self.layer.shadowRadius = 10.0;
  161. }
  162. -(void)addShadowWithColor:(UIColor *)color width:(CGFloat)width opacity:(CGFloat)opacity{
  163. // 阴影颜色
  164. self.layer.shadowColor = color.CGColor;
  165. // 阴影偏移,默认(0, -3)
  166. self.layer.shadowOffset = CGSizeMake(0,0);
  167. // 阴影透明度,默认0
  168. self.layer.shadowOpacity = opacity;
  169. // 阴影半径,默认3
  170. self.layer.shadowRadius = width;
  171. }
  172. -(void)addMovingShadow
  173. {
  174. static float step = 0.0;
  175. if (step>20.0) {
  176. step = 0.0;
  177. }
  178. self.layer.shadowOpacity = 0.4;
  179. self.layer.shadowRadius = 1.5;
  180. self.layer.shadowOffset = CGSizeMake(0, 0);
  181. UIBezierPath *path = [UIBezierPath bezierPath];
  182. CGPoint p1 = CGPointMake(0.0, 0.0+self.frame.size.height);
  183. CGPoint p2 = CGPointMake(0.0+self.frame.size.width, p1.y);
  184. CGPoint c1 = CGPointMake((p1.x+p2.x)/4 , p1.y+step);
  185. CGPoint c2 = CGPointMake(c1.x*3, c1.y);
  186. [path moveToPoint:p1];
  187. [path addCurveToPoint:p2 controlPoint1:c1 controlPoint2:c2];
  188. self.layer.shadowPath = path.CGPath;
  189. step += 0.1;
  190. [self performSelector:@selector(addMovingShadow) withObject:nil afterDelay:1.0/30.0];
  191. }
  192. -(void)removeShadow
  193. {
  194. self.layer.shadowOpacity =0;
  195. self.layer.shadowRadius = 0;
  196. self.layer.shadowOffset = CGSizeMake(0, 0);
  197. UIBezierPath *path = [UIBezierPath bezierPath];
  198. CGPoint p1 = CGPointMake(0.0, 0.0);
  199. CGPoint p2 = CGPointMake(0.0+self.frame.size.width, p1.y);
  200. CGPoint c1 = CGPointMake(0 , 0);
  201. CGPoint c2 = CGPointMake(0, 0);
  202. [path moveToPoint:p1];
  203. [path addCurveToPoint:p2 controlPoint1:c1 controlPoint2:c2];
  204. self.layer.shadowPath = path.CGPath;
  205. }
  206. //针对给定的坐标系居中
  207. - (void)centerInRect:(CGRect)rect;
  208. {
  209. //如果参数是小数,则求最大的整数但不大于本身.
  210. //CGRectGetMidX获取中心点的X轴坐标
  211. [self setCenter:CGPointMake(floorf(CGRectGetMidX(rect)) + ((int)floorf([self width]) % 2 ? .5 : 0) , floorf(CGRectGetMidY(rect)) + ((int)floorf([self height]) % 2 ? .5 : 0))];
  212. }
  213. //针对给定的坐标系纵向居中
  214. - (void)centerVerticallyInRect:(CGRect)rect;
  215. {
  216. [self setCenter:CGPointMake([self center].x, floorf(CGRectGetMidY(rect)) + ((int)floorf([self height]) % 2 ? .5 : 0))];
  217. }
  218. //针对给定的坐标系横向居中
  219. - (void)centerHorizontallyInRect:(CGRect)rect;
  220. {
  221. [self setCenter:CGPointMake(floorf(CGRectGetMidX(rect)) + ((int)floorf([self width]) % 2 ? .5 : 0), [self center].y)];
  222. }
  223. //相对父视图居中
  224. - (void)centerInSuperView;
  225. {
  226. [self centerInRect:[[self superview] bounds]];
  227. }
  228. - (void)centerVerticallyInSuperView;
  229. {
  230. [self centerVerticallyInRect:[[self superview] bounds]];
  231. }
  232. - (void)centerHorizontallyInSuperView;
  233. {
  234. [self centerHorizontallyInRect:[[self superview] bounds]];
  235. }
  236. //同一父视图的兄弟视图水平居中
  237. - (void)centerHorizontallyBelow:(UIView *)view padding:(CGFloat)padding;
  238. {
  239. // for now, could use screen relative positions.
  240. NSAssert([self superview] == [view superview], @"views must have the same parent");
  241. [self setCenter:CGPointMake([view center].x,
  242. floorf(padding + CGRectGetMaxY([view frame]) + ([self height] / 2)))];
  243. }
  244. - (void)centerHorizontallyBelow:(UIView *)view;
  245. {
  246. [self centerHorizontallyBelow:view padding:0];
  247. }
  248. - (void)setFrameSize:(CGSize)newSize
  249. {
  250. CGRect f = self.frame;
  251. f.size = newSize;
  252. self.frame = f;
  253. }
  254. - (void)setFrameWidth:(CGFloat)newWidth {
  255. CGRect f = self.frame;
  256. f.size.width = newWidth;
  257. self.frame = f;
  258. }
  259. - (void)setFrameHeight:(CGFloat)newHeight {
  260. CGRect f = self.frame;
  261. f.size.height = newHeight;
  262. self.frame = f;
  263. }
  264. - (void)setFrameOrigin:(CGPoint)newOrigin
  265. {
  266. CGRect f = self.frame;
  267. f.origin = newOrigin;
  268. self.frame = f;
  269. }
  270. - (void)setFrameOriginX:(CGFloat)newX {
  271. CGRect f = self.frame;
  272. f.origin.x = newX;
  273. self.frame = f;
  274. }
  275. - (void)setFrameOriginY:(CGFloat)newY {
  276. CGRect f = self.frame;
  277. f.origin.y = newY;
  278. self.frame = f;
  279. }
  280. - (void)addSizeWidth:(CGFloat)newWidth {
  281. CGRect f = self.frame;
  282. f.size.width += newWidth;
  283. self.frame = f;
  284. }
  285. - (void)addSizeHeight:(CGFloat)newHeight {
  286. CGRect f = self.frame;
  287. f.size.height += newHeight;
  288. self.frame = f;
  289. }
  290. - (void)addOriginX:(CGFloat)newX {
  291. CGRect f = self.frame;
  292. f.origin.x += newX;
  293. self.frame = f;
  294. }
  295. - (void)addOriginY:(CGFloat)newY {
  296. CGRect f = self.frame;
  297. f.origin.y += newY;
  298. self.frame = f;
  299. }
  300. - (void)cornerRadius:(CGFloat)cornerRadius withUIViewRadiusType:(UIViewRadiusType)type
  301. {
  302. self.clipsToBounds = YES;
  303. UIBezierPath *cornerRadiusPath;
  304. switch (type) {
  305. case UIViewRadiusTypeNone:
  306. {
  307. cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
  308. }
  309. break;
  310. case UIViewRadiusTypeTopLeft:
  311. {
  312. cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
  313. }
  314. break;
  315. case UIViewRadiusTypeTopRight:
  316. {
  317. cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
  318. }
  319. break;
  320. case UIViewRadiusTypeBottomLeft:
  321. {
  322. cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
  323. }
  324. break;
  325. case UIViewRadiusTypeBottomRight:
  326. {
  327. cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
  328. }
  329. break;
  330. default:
  331. break;
  332. }
  333. CAShapeLayer *cornerRadiusLayer = [ [CAShapeLayer alloc ] init];
  334. cornerRadiusLayer.frame = self.bounds;
  335. cornerRadiusLayer.path = cornerRadiusPath.CGPath; self.layer.mask = cornerRadiusLayer;
  336. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(15, 15)];
  337. }
  338. @end