猎豆优选

UIView+DCRolling.m 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #import "UIView+DCRolling.h"
  2. @implementation UIView (DCRolling)
  3. - (CGFloat)dc_x
  4. {
  5. return self.frame.origin.x;
  6. }
  7. - (void)setDc_x:(CGFloat)dc_x
  8. {
  9. CGRect dcFrame = self.frame;
  10. dcFrame.origin.x = dc_x;
  11. self.frame = dcFrame;
  12. }
  13. -(CGFloat)dc_y
  14. {
  15. return self.frame.origin.y;
  16. }
  17. -(void)setDc_y:(CGFloat)dc_y
  18. {
  19. CGRect dcFrame = self.frame;
  20. dcFrame.origin.y = dc_y;
  21. self.frame = dcFrame;
  22. }
  23. -(CGPoint)dc_origin
  24. {
  25. return self.frame.origin;
  26. }
  27. -(void)setDc_origin:(CGPoint)dc_origin
  28. {
  29. CGRect dcFrame = self.frame;
  30. dcFrame.origin = dc_origin;
  31. self.frame = dcFrame;
  32. }
  33. -(CGFloat)dc_width
  34. {
  35. return self.frame.size.width;
  36. }
  37. -(void)setDc_width:(CGFloat)dc_width
  38. {
  39. CGRect dcFrame = self.frame;
  40. dcFrame.size.width = dc_width;
  41. self.frame = dcFrame;
  42. }
  43. -(CGFloat)dc_height
  44. {
  45. return self.frame.size.height;
  46. }
  47. -(void)setDc_height:(CGFloat)dc_height
  48. {
  49. CGRect dcFrame = self.frame;
  50. dcFrame.size.height = dc_height;
  51. self.frame = dcFrame;
  52. }
  53. -(CGSize)dc_size
  54. {
  55. return self.frame.size;
  56. }
  57. - (void)setDc_size:(CGSize)dc_size
  58. {
  59. CGRect dcFrame = self.frame;
  60. dcFrame.size = dc_size;
  61. self.frame = dcFrame;
  62. }
  63. -(CGFloat)dc_centerX{
  64. return self.center.x;
  65. }
  66. -(void)setDc_centerX:(CGFloat)dc_centerX{
  67. CGPoint dcFrmae = self.center;
  68. dcFrmae.x = dc_centerX;
  69. self.center = dcFrmae;
  70. }
  71. -(CGFloat)dc_centerY{
  72. return self.center.y;
  73. }
  74. -(void)setDc_centerY:(CGFloat)dc_centerY{
  75. CGPoint dcFrame = self.center;
  76. dcFrame.y = dc_centerY;
  77. self.center = dcFrame;
  78. }
  79. - (CGFloat)dc_right{
  80. return CGRectGetMaxX(self.frame);
  81. }
  82. - (CGFloat)dc_bottom{
  83. return CGRectGetMaxY(self.frame);
  84. }
  85. - (void)setDc_right:(CGFloat)dc_right{
  86. self.dc_x = dc_right - self.dc_width;
  87. }
  88. - (void)setDc_bottom:(CGFloat)dc_bottom{
  89. self.dc_y = dc_bottom - self.dc_height;
  90. }
  91. - (BOOL)intersectWithView:(UIView *)view{
  92. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  93. CGRect selfRect = [self convertRect:self.bounds toView:window];
  94. CGRect viewRect = [view convertRect:view.bounds toView:window];
  95. return CGRectIntersectsRect(selfRect, viewRect);
  96. }
  97. - (BOOL)isShowingOnKeyWindow {
  98. // 主窗口
  99. UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
  100. // 以主窗口左上角为坐标原点, 计算self的矩形框
  101. CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
  102. CGRect winBounds = keyWindow.bounds;
  103. // 主窗口的bounds 和 self的矩形框 是否有重叠
  104. BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
  105. return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;
  106. }
  107. +(instancetype)dc_viewFromXib
  108. {
  109. return [[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil].firstObject;
  110. }
  111. - (CGSize)labelAutoCalculateRectWith:(NSString *)text FontSize:(CGFloat)fontSize MaxSize:(CGSize)maxSize
  112. {
  113. NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc]init];
  114. paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
  115. NSDictionary * attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize], NSParagraphStyleAttributeName:paragraphStyle.copy};
  116. CGSize labelSize = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
  117. labelSize.height = ceil(labelSize.height);
  118. labelSize.width = ceil(labelSize.width);
  119. return labelSize;
  120. }
  121. -(id)dc_chageControlCircularWith:(id)anyControl AndSetCornerRadius:(NSInteger)radius SetBorderWidth:(NSInteger)width SetBorderColor:(UIColor *)borderColor canMasksToBounds:(BOOL)can
  122. {
  123. CALayer *icon_layer=[anyControl layer];
  124. [icon_layer setCornerRadius:radius];
  125. [icon_layer setBorderWidth:width];
  126. [icon_layer setBorderColor:[borderColor CGColor]];
  127. [icon_layer setMasksToBounds:can];
  128. return anyControl;
  129. }
  130. @end