UIView+Layout.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. //
  2. // UIView+Layout.m
  3. // CommonLibrary
  4. //
  5. // Created by Alexi Chen on 2/28/13.
  6. // Copyright (c) 2013 AlexiChen. All rights reserved.
  7. //
  8. #import "UIView+Layout.h"
  9. #import <YYKit/YYkit.h>
  10. @implementation UIView (Layout)
  11. @dynamic top;
  12. @dynamic bottom;
  13. @dynamic left;
  14. @dynamic right;
  15. @dynamic width;
  16. @dynamic height;
  17. @dynamic size;
  18. @dynamic x;
  19. @dynamic y;
  20. - (CGFloat)tz_left {
  21. return self.frame.origin.x;
  22. }
  23. - (void)setTz_left:(CGFloat)x {
  24. CGRect frame = self.frame;
  25. frame.origin.x = x;
  26. self.frame = frame;
  27. }
  28. - (CGFloat)tz_top {
  29. return self.frame.origin.y;
  30. }
  31. - (void)setTz_top:(CGFloat)y {
  32. CGRect frame = self.frame;
  33. frame.origin.y = y;
  34. self.frame = frame;
  35. }
  36. - (CGFloat)tz_right {
  37. return self.frame.origin.x + self.frame.size.width;
  38. }
  39. - (void)setTz_right:(CGFloat)right {
  40. CGRect frame = self.frame;
  41. frame.origin.x = right - frame.size.width;
  42. self.frame = frame;
  43. }
  44. - (CGFloat)tz_bottom {
  45. return self.frame.origin.y + self.frame.size.height;
  46. }
  47. - (void)setTz_bottom:(CGFloat)bottom {
  48. CGRect frame = self.frame;
  49. frame.origin.y = bottom - frame.size.height;
  50. self.frame = frame;
  51. }
  52. - (CGFloat)tz_width {
  53. return self.frame.size.width;
  54. }
  55. - (void)setTz_width:(CGFloat)width {
  56. CGRect frame = self.frame;
  57. frame.size.width = width;
  58. self.frame = frame;
  59. }
  60. - (CGFloat)tz_height {
  61. return self.frame.size.height;
  62. }
  63. - (void)setTz_height:(CGFloat)height {
  64. CGRect frame = self.frame;
  65. frame.size.height = height;
  66. self.frame = frame;
  67. }
  68. - (CGFloat)tz_centerX {
  69. return self.center.x;
  70. }
  71. - (void)setTz_centerX:(CGFloat)centerX {
  72. self.center = CGPointMake(centerX, self.center.y);
  73. }
  74. - (CGFloat)tz_centerY {
  75. return self.center.y;
  76. }
  77. - (void)setTz_centerY:(CGFloat)centerY {
  78. self.center = CGPointMake(self.center.x, centerY);
  79. }
  80. - (CGPoint)tz_origin {
  81. return self.frame.origin;
  82. }
  83. - (void)setTz_origin:(CGPoint)origin {
  84. CGRect frame = self.frame;
  85. frame.origin = origin;
  86. self.frame = frame;
  87. }
  88. - (CGSize)tz_size {
  89. return self.frame.size;
  90. }
  91. - (void)setTz_size:(CGSize)size {
  92. CGRect frame = self.frame;
  93. frame.size = size;
  94. self.frame = frame;
  95. }
  96. + (void)showOscillatoryAnimationWithLayer:(CALayer *)layer type:(TZOscillatoryAnimationType)type{
  97. NSNumber *animationScale1 = type == TZOscillatoryAnimationToBigger ? @(1.15) : @(0.5);
  98. NSNumber *animationScale2 = type == TZOscillatoryAnimationToBigger ? @(0.92) : @(1.15);
  99. [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
  100. [layer setValue:animationScale1 forKeyPath:@"transform.scale"];
  101. } completion:^(BOOL finished) {
  102. [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
  103. [layer setValue:animationScale2 forKeyPath:@"transform.scale"];
  104. } completion:^(BOOL finished) {
  105. [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
  106. [layer setValue:@(1.0) forKeyPath:@"transform.scale"];
  107. } completion:nil];
  108. }];
  109. }];
  110. }
  111. - (CGFloat)top
  112. {
  113. return self.frame.origin.y;
  114. }
  115. - (void)setTop:(CGFloat)top
  116. {
  117. CGRect frame = self.frame;
  118. frame.origin.y = top;
  119. self.frame = frame;
  120. }
  121. - (CGFloat)left
  122. {
  123. return self.frame.origin.x;
  124. }
  125. - (void)setLeft:(CGFloat)left
  126. {
  127. CGRect frame = self.frame;
  128. frame.origin.x = left;
  129. self.frame = frame;
  130. }
  131. - (CGFloat)bottom
  132. {
  133. return self.frame.size.height + self.frame.origin.y;
  134. }
  135. - (void)setBottom:(CGFloat)bottom
  136. {
  137. CGRect frame = self.frame;
  138. frame.origin.y = bottom - frame.size.height;
  139. self.frame = frame;
  140. }
  141. - (CGFloat)right
  142. {
  143. return self.frame.size.width + self.frame.origin.x;
  144. }
  145. - (void)setRight:(CGFloat)right
  146. {
  147. CGRect frame = self.frame;
  148. frame.origin.x = right - frame.size.width;
  149. self.frame = frame;
  150. }
  151. - (CGFloat)x
  152. {
  153. return self.frame.origin.x;
  154. }
  155. - (void)setX:(CGFloat)value
  156. {
  157. CGRect frame = self.frame;
  158. frame.origin.x = value;
  159. self.frame = frame;
  160. }
  161. - (CGFloat)y
  162. {
  163. return self.frame.origin.y;
  164. }
  165. - (void)setY:(CGFloat)value
  166. {
  167. CGRect frame = self.frame;
  168. frame.origin.y = value;
  169. self.frame = frame;
  170. }
  171. - (CGPoint)origin
  172. {
  173. return self.frame.origin;
  174. }
  175. - (void)setOrigin:(CGPoint)origin
  176. {
  177. CGRect frame = self.frame;
  178. frame.origin = origin;
  179. self.frame = frame;
  180. }
  181. - (CGFloat)centerX
  182. {
  183. return self.center.x;
  184. }
  185. - (void)setCenterX:(CGFloat)centerX
  186. {
  187. CGPoint center = self.center;
  188. center.x = centerX;
  189. self.center = center;
  190. }
  191. - (CGFloat)centerY
  192. {
  193. return self.center.y;
  194. }
  195. - (void)setCenterY:(CGFloat)centerY
  196. {
  197. CGPoint center = self.center;
  198. center.y = centerY;
  199. self.center = center;
  200. }
  201. - (CGFloat)width
  202. {
  203. return self.frame.size.width;
  204. }
  205. - (void)setWidth:(CGFloat)width
  206. {
  207. CGRect frame = self.frame;
  208. frame.size.width = width;
  209. self.frame = frame;
  210. }
  211. - (CGFloat)height
  212. {
  213. return self.frame.size.height;
  214. }
  215. - (void)setHeight:(CGFloat)height
  216. {
  217. CGRect frame = self.frame;
  218. frame.size.height = height;
  219. self.frame = frame;
  220. }
  221. - (CGSize)size
  222. {
  223. return self.frame.size;
  224. }
  225. - (void)setSize:(CGSize)size
  226. {
  227. CGRect frame = self.frame;
  228. frame.size = size;
  229. self.frame = frame;
  230. }
  231. //- (id)init
  232. //{
  233. // if (self = [self initWithFrame:CGRectZero]) {
  234. //
  235. // if (![self isAutoLayout])
  236. // {
  237. // [self addOwnViews];
  238. // [self configOwnViews];
  239. // }
  240. // else
  241. // {
  242. // [self autoLayoutSubViews];
  243. // }
  244. //
  245. // }
  246. // return self;
  247. //}
  248. // (void)load
  249. //{
  250. // static dispatch_once_t onceToken;
  251. // dispatch_once(&onceToken, ^{
  252. //
  253. // [UIView swizzleClassMethod:@selector(init) with:@selector(fk_init)];
  254. //// [self swizzleInstanceSelector:@selector(modalPresentationStyle) withSelector:@selector(uiviewController_modalPresentationStyle)];
  255. // });
  256. //}
  257. //- (instancetype)fk_init
  258. //{
  259. // self = [super init];
  260. //// if (self) {
  261. // if (![self isAutoLayout])
  262. // {
  263. // [self addOwnViews];
  264. // [self configOwnViews];
  265. // }
  266. // else
  267. // {
  268. // [self autoLayoutSubViews];
  269. // }
  270. //// }
  271. // return self;
  272. //}
  273. - (BOOL)isAutoLayout
  274. {
  275. return NO;
  276. }
  277. - (void)autoLayoutSubViews
  278. {
  279. }
  280. - (void)addOwnViews
  281. {
  282. // 添加自己的控件
  283. }
  284. - (void)configOwnViews
  285. {
  286. // 初始化控件的值
  287. }
  288. //- (void)configWith:(NSMutableDictionary *)jsonDic
  289. //{
  290. //
  291. //}
  292. - (void)setFrameAndLayout:(CGRect)rect
  293. {
  294. self.frame = rect;
  295. if (self.bounds.size.width != 0 && self.bounds.size.height != 0)
  296. {
  297. [self relayoutFrameOfSubViews];
  298. }
  299. }
  300. - (void)relayoutFrameOfSubViews
  301. {
  302. // do nothing here
  303. }
  304. - (void)addBottomLine:(CGRect)rect
  305. {
  306. [self addBottomLine:kLightGrayColor inRect:rect];
  307. }
  308. - (void)addBottomLine:(UIColor *)color inRect:(CGRect)rect
  309. {
  310. CGContextRef context = UIGraphicsGetCurrentContext();
  311. UIGraphicsPushContext(context);
  312. //Set the stroke (pen) color
  313. CGContextSetStrokeColorWithColor(context, color.CGColor);
  314. CGContextSetLineWidth(context, 1.0);
  315. CGContextBeginPath(context);
  316. CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect));
  317. CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
  318. CGContextStrokePath(context);
  319. UIGraphicsPopContext();
  320. }
  321. @end
  322. @implementation UIView (ShakeAnimation)
  323. - (void)shake
  324. {
  325. CGFloat t =4.0;
  326. CGAffineTransform translateRight = CGAffineTransformTranslate(CGAffineTransformIdentity, t, 0.0);
  327. CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, 0.0);
  328. self.transform = translateLeft;
  329. [UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
  330. [UIView setAnimationRepeatCount:2.0];
  331. self.transform = translateRight;
  332. } completion:^(BOOL finished) {
  333. if(finished)
  334. {
  335. [UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
  336. self.transform =CGAffineTransformIdentity;
  337. } completion:NULL];
  338. }
  339. }];
  340. }
  341. @end