UIView+LZBViewFrame.m 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // demo地址:https://github.com/lzbgithubcode/LZBKeyBoardView.git
  2. #import "UIView+LZBViewFrame.h"
  3. @implementation UIView (LZBViewFrame)
  4. -(void)setLZB_x:(CGFloat)LZB_x
  5. {
  6. CGRect rect =self.frame;
  7. rect.origin.x =LZB_x;
  8. self.frame =rect;
  9. }
  10. -(void)setLZB_y:(CGFloat)LZB_y
  11. {
  12. CGRect rect =self.frame;
  13. rect.origin.y =LZB_y;
  14. self.frame =rect;
  15. }
  16. - (void)setLZB_width:(CGFloat)LZB_width
  17. {
  18. CGRect rect =self.frame;
  19. rect.size.width =LZB_width;
  20. self.frame =rect;
  21. }
  22. - (void)setLZB_heigth:(CGFloat)LZB_heigth
  23. {
  24. CGRect rect =self.frame;
  25. rect.size.height =LZB_heigth;
  26. self.frame =rect;
  27. }
  28. - (CGFloat)LZB_width
  29. {
  30. return self.frame.size.width;
  31. }
  32. - (CGFloat)LZB_heigth
  33. {
  34. return self.frame.size.height;
  35. }
  36. - (CGFloat)LZB_x
  37. {
  38. return self.frame.origin.x;
  39. }
  40. - (CGFloat)LZB_y
  41. {
  42. return self.frame.origin.y;
  43. }
  44. - (void)setLZB_centerX:(CGFloat)LZB_centerX
  45. {
  46. CGPoint center =self.center;
  47. center.x =LZB_centerX;
  48. self.center =center;
  49. }
  50. - (void)setLZB_centerY:(CGFloat)LZB_centerY
  51. {
  52. CGPoint center =self.center;
  53. center.y =LZB_centerY;
  54. self.center =center;
  55. }
  56. -(CGFloat)LZB_centerX
  57. {
  58. return self.center.x;
  59. }
  60. - (CGFloat)LZB_centerY
  61. {
  62. return self.center.y;
  63. }
  64. #pragma mark - API
  65. // 判断View是否显示在屏幕上
  66. - (BOOL)isDisplayedInScreen
  67. {
  68. if (self == nil) {
  69. return FALSE;
  70. }
  71. CGRect screenRect = [UIScreen mainScreen].bounds;
  72. // 转换view对应window的Rect
  73. CGRect rect = [self convertRect:self.frame fromView:nil];
  74. if (CGRectIsEmpty(rect) || CGRectIsNull(rect)) {
  75. return FALSE;
  76. }
  77. // 若view 隐藏
  78. if (self.hidden) {
  79. return FALSE;
  80. }
  81. // 若没有superview
  82. if (self.superview == nil) {
  83. return FALSE;
  84. }
  85. // 若size为CGrectZero
  86. if (CGSizeEqualToSize(rect.size, CGSizeZero)) {
  87. return FALSE;
  88. }
  89. // 获取 该view与window 交叉的 Rect
  90. CGRect intersectionRect = CGRectIntersection(rect, screenRect);
  91. if (CGRectIsEmpty(intersectionRect) || CGRectIsNull(intersectionRect)) {
  92. return FALSE;
  93. }
  94. return TRUE;
  95. }
  96. @end