UIView+YYAdd.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // UIView+YYAdd.m
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 13/4/3.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import "UIView+YYAdd.h"
  12. #import <QuartzCore/QuartzCore.h>
  13. #import "YYKitMacro.h"
  14. YYSYNTH_DUMMY_CLASS(UIView_YYAdd)
  15. @implementation UIView (YYAdd)
  16. - (UIImage *)snapshotImage {
  17. // 传入的View.frame.size是0的话,直接返回nil,防止 UIGraphicsBeginImageContext() 传入0,导致崩溃
  18. if (CGSizeEqualToSize(self.bounds.size, CGSizeZero)) {
  19. return nil;
  20. }
  21. UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0);
  22. [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  23. UIImage *snap = UIGraphicsGetImageFromCurrentImageContext();
  24. UIGraphicsEndImageContext();
  25. return snap;
  26. }
  27. - (UIImage *)snapshotImageAfterScreenUpdates:(BOOL)afterUpdates {
  28. if (![self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
  29. return [self snapshotImage];
  30. }
  31. // 传入的View.frame.size是0的话,直接返回nil,防止 UIGraphicsBeginImageContext() 传入0,导致崩溃
  32. if (CGSizeEqualToSize(self.bounds.size, CGSizeZero)) {
  33. return nil;
  34. }
  35. UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0);
  36. [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:afterUpdates];
  37. UIImage *snap = UIGraphicsGetImageFromCurrentImageContext();
  38. UIGraphicsEndImageContext();
  39. return snap;
  40. }
  41. - (NSData *)snapshotPDF {
  42. CGRect bounds = self.bounds;
  43. NSMutableData *data = [NSMutableData data];
  44. CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef)data);
  45. CGContextRef context = CGPDFContextCreate(consumer, &bounds, NULL);
  46. CGDataConsumerRelease(consumer);
  47. if (!context) return nil;
  48. CGPDFContextBeginPage(context, NULL);
  49. CGContextTranslateCTM(context, 0, bounds.size.height);
  50. CGContextScaleCTM(context, 1.0, -1.0);
  51. [self.layer renderInContext:context];
  52. CGPDFContextEndPage(context);
  53. CGPDFContextClose(context);
  54. CGContextRelease(context);
  55. return data;
  56. }
  57. - (void)setLayerShadow:(UIColor*)color offset:(CGSize)offset radius:(CGFloat)radius {
  58. self.layer.shadowColor = color.CGColor;
  59. self.layer.shadowOffset = offset;
  60. self.layer.shadowRadius = radius;
  61. self.layer.shadowOpacity = 1;
  62. self.layer.shouldRasterize = YES;
  63. self.layer.rasterizationScale = [UIScreen mainScreen].scale;
  64. }
  65. - (void)removeAllSubviews {
  66. //[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  67. while (self.subviews.count) {
  68. [self.subviews.lastObject removeFromSuperview];
  69. }
  70. }
  71. - (UIViewController *)viewController {
  72. for (UIView *view = self; view; view = view.superview) {
  73. UIResponder *nextResponder = [view nextResponder];
  74. if ([nextResponder isKindOfClass:[UIViewController class]]) {
  75. return (UIViewController *)nextResponder;
  76. }
  77. }
  78. return nil;
  79. }
  80. - (CGFloat)visibleAlpha {
  81. if ([self isKindOfClass:[UIWindow class]]) {
  82. if (self.hidden) return 0;
  83. return self.alpha;
  84. }
  85. if (!self.window) return 0;
  86. CGFloat alpha = 1;
  87. UIView *v = self;
  88. while (v) {
  89. if (v.hidden) {
  90. alpha = 0;
  91. break;
  92. }
  93. alpha *= v.alpha;
  94. v = v.superview;
  95. }
  96. return alpha;
  97. }
  98. - (CGPoint)convertPoint:(CGPoint)point toViewOrWindow:(UIView *)view {
  99. if (!view) {
  100. if ([self isKindOfClass:[UIWindow class]]) {
  101. return [((UIWindow *)self) convertPoint:point toWindow:nil];
  102. } else {
  103. return [self convertPoint:point toView:nil];
  104. }
  105. }
  106. UIWindow *from = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window;
  107. UIWindow *to = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window;
  108. if ((!from || !to) || (from == to)) return [self convertPoint:point toView:view];
  109. point = [self convertPoint:point toView:from];
  110. point = [to convertPoint:point fromWindow:from];
  111. point = [view convertPoint:point fromView:to];
  112. return point;
  113. }
  114. - (CGPoint)convertPoint:(CGPoint)point fromViewOrWindow:(UIView *)view {
  115. if (!view) {
  116. if ([self isKindOfClass:[UIWindow class]]) {
  117. return [((UIWindow *)self) convertPoint:point fromWindow:nil];
  118. } else {
  119. return [self convertPoint:point fromView:nil];
  120. }
  121. }
  122. UIWindow *from = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window;
  123. UIWindow *to = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window;
  124. if ((!from || !to) || (from == to)) return [self convertPoint:point fromView:view];
  125. point = [from convertPoint:point fromView:view];
  126. point = [to convertPoint:point fromWindow:from];
  127. point = [self convertPoint:point fromView:to];
  128. return point;
  129. }
  130. - (CGRect)convertRect:(CGRect)rect toViewOrWindow:(UIView *)view {
  131. if (!view) {
  132. if ([self isKindOfClass:[UIWindow class]]) {
  133. return [((UIWindow *)self) convertRect:rect toWindow:nil];
  134. } else {
  135. return [self convertRect:rect toView:nil];
  136. }
  137. }
  138. UIWindow *from = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window;
  139. UIWindow *to = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window;
  140. if (!from || !to) return [self convertRect:rect toView:view];
  141. if (from == to) return [self convertRect:rect toView:view];
  142. rect = [self convertRect:rect toView:from];
  143. rect = [to convertRect:rect fromWindow:from];
  144. rect = [view convertRect:rect fromView:to];
  145. return rect;
  146. }
  147. - (CGRect)convertRect:(CGRect)rect fromViewOrWindow:(UIView *)view {
  148. if (!view) {
  149. if ([self isKindOfClass:[UIWindow class]]) {
  150. return [((UIWindow *)self) convertRect:rect fromWindow:nil];
  151. } else {
  152. return [self convertRect:rect fromView:nil];
  153. }
  154. }
  155. UIWindow *from = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window;
  156. UIWindow *to = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window;
  157. if ((!from || !to) || (from == to)) return [self convertRect:rect fromView:view];
  158. rect = [from convertRect:rect fromView:view];
  159. rect = [to convertRect:rect fromWindow:from];
  160. rect = [self convertRect:rect fromView:to];
  161. return rect;
  162. }
  163. - (CGFloat)left {
  164. return self.frame.origin.x;
  165. }
  166. - (void)setLeft:(CGFloat)x {
  167. CGRect frame = self.frame;
  168. frame.origin.x = x;
  169. self.frame = frame;
  170. }
  171. - (CGFloat)top {
  172. return self.frame.origin.y;
  173. }
  174. - (void)setTop:(CGFloat)y {
  175. CGRect frame = self.frame;
  176. frame.origin.y = y;
  177. self.frame = frame;
  178. }
  179. - (CGFloat)right {
  180. return self.frame.origin.x + self.frame.size.width;
  181. }
  182. - (void)setRight:(CGFloat)right {
  183. CGRect frame = self.frame;
  184. frame.origin.x = right - frame.size.width;
  185. self.frame = frame;
  186. }
  187. - (CGFloat)bottom {
  188. return self.frame.origin.y + self.frame.size.height;
  189. }
  190. - (void)setBottom:(CGFloat)bottom {
  191. CGRect frame = self.frame;
  192. frame.origin.y = bottom - frame.size.height;
  193. self.frame = frame;
  194. }
  195. - (CGFloat)width {
  196. return self.frame.size.width;
  197. }
  198. - (void)setWidth:(CGFloat)width {
  199. CGRect frame = self.frame;
  200. frame.size.width = width;
  201. self.frame = frame;
  202. }
  203. - (CGFloat)height {
  204. return self.frame.size.height;
  205. }
  206. - (void)setHeight:(CGFloat)height {
  207. CGRect frame = self.frame;
  208. frame.size.height = height;
  209. self.frame = frame;
  210. }
  211. - (CGFloat)centerX {
  212. return self.center.x;
  213. }
  214. - (void)setCenterX:(CGFloat)centerX {
  215. self.center = CGPointMake(centerX, self.center.y);
  216. }
  217. - (CGFloat)centerY {
  218. return self.center.y;
  219. }
  220. - (void)setCenterY:(CGFloat)centerY {
  221. self.center = CGPointMake(self.center.x, centerY);
  222. }
  223. - (CGPoint)origin {
  224. return self.frame.origin;
  225. }
  226. - (void)setOrigin:(CGPoint)origin {
  227. CGRect frame = self.frame;
  228. frame.origin = origin;
  229. self.frame = frame;
  230. }
  231. - (CGSize)size {
  232. return self.frame.size;
  233. }
  234. - (void)setSize:(CGSize)size {
  235. CGRect frame = self.frame;
  236. frame.size = size;
  237. self.frame = frame;
  238. }
  239. @end