UIView+YYAdd.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // UIView+YYAdd.h
  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 <UIKit/UIKit.h>
  12. NS_ASSUME_NONNULL_BEGIN
  13. /**
  14. Provides extensions for `UIView`.
  15. */
  16. @interface UIView (YYAdd)
  17. /**
  18. Create a snapshot image of the complete view hierarchy.
  19. */
  20. - (nullable UIImage *)snapshotImage;
  21. /**
  22. Create a snapshot image of the complete view hierarchy.
  23. @discussion It's faster than "snapshotImage", but may cause screen updates.
  24. See -[UIView drawViewHierarchyInRect:afterScreenUpdates:] for more information.
  25. */
  26. - (nullable UIImage *)snapshotImageAfterScreenUpdates:(BOOL)afterUpdates;
  27. /**
  28. Create a snapshot PDF of the complete view hierarchy.
  29. */
  30. - (nullable NSData *)snapshotPDF;
  31. /**
  32. Shortcut to set the view.layer's shadow
  33. @param color Shadow Color
  34. @param offset Shadow offset
  35. @param radius Shadow radius
  36. */
  37. - (void)setLayerShadow:(nullable UIColor*)color offset:(CGSize)offset radius:(CGFloat)radius;
  38. /**
  39. Remove all subviews.
  40. @warning Never call this method inside your view's drawRect: method.
  41. */
  42. - (void)removeAllSubviews;
  43. /**
  44. Returns the view's view controller (may be nil).
  45. */
  46. @property (nullable, nonatomic, readonly) UIViewController *viewController;
  47. /**
  48. Returns the visible alpha on screen, taking into account superview and window.
  49. */
  50. @property (nonatomic, readonly) CGFloat visibleAlpha;
  51. /**
  52. Converts a point from the receiver's coordinate system to that of the specified view or window.
  53. @param point A point specified in the local coordinate system (bounds) of the receiver.
  54. @param view The view or window into whose coordinate system point is to be converted.
  55. If view is nil, this method instead converts to window base coordinates.
  56. @return The point converted to the coordinate system of view.
  57. */
  58. - (CGPoint)convertPoint:(CGPoint)point toViewOrWindow:(nullable UIView *)view;
  59. /**
  60. Converts a point from the coordinate system of a given view or window to that of the receiver.
  61. @param point A point specified in the local coordinate system (bounds) of view.
  62. @param view The view or window with point in its coordinate system.
  63. If view is nil, this method instead converts from window base coordinates.
  64. @return The point converted to the local coordinate system (bounds) of the receiver.
  65. */
  66. - (CGPoint)convertPoint:(CGPoint)point fromViewOrWindow:(nullable UIView *)view;
  67. /**
  68. Converts a rectangle from the receiver's coordinate system to that of another view or window.
  69. @param rect A rectangle specified in the local coordinate system (bounds) of the receiver.
  70. @param view The view or window that is the target of the conversion operation. If view is nil, this method instead converts to window base coordinates.
  71. @return The converted rectangle.
  72. */
  73. - (CGRect)convertRect:(CGRect)rect toViewOrWindow:(nullable UIView *)view;
  74. /**
  75. Converts a rectangle from the coordinate system of another view or window to that of the receiver.
  76. @param rect A rectangle specified in the local coordinate system (bounds) of view.
  77. @param view The view or window with rect in its coordinate system.
  78. If view is nil, this method instead converts from window base coordinates.
  79. @return The converted rectangle.
  80. */
  81. - (CGRect)convertRect:(CGRect)rect fromViewOrWindow:(nullable UIView *)view;
  82. @property (nonatomic) CGFloat left; ///< Shortcut for frame.origin.x.
  83. @property (nonatomic) CGFloat top; ///< Shortcut for frame.origin.y
  84. @property (nonatomic) CGFloat right; ///< Shortcut for frame.origin.x + frame.size.width
  85. @property (nonatomic) CGFloat bottom; ///< Shortcut for frame.origin.y + frame.size.height
  86. @property (nonatomic) CGFloat width; ///< Shortcut for frame.size.width.
  87. @property (nonatomic) CGFloat height; ///< Shortcut for frame.size.height.
  88. @property (nonatomic) CGFloat centerX; ///< Shortcut for center.x
  89. @property (nonatomic) CGFloat centerY; ///< Shortcut for center.y
  90. @property (nonatomic) CGPoint origin; ///< Shortcut for frame.origin.
  91. @property (nonatomic) CGSize size; ///< Shortcut for frame.size.
  92. @end
  93. NS_ASSUME_NONNULL_END