iOS开发-很有用的UIView分类

很有用的UIView分类,可以用来方便访问视图坐标X,Y,中心点,宽度和高度等等

UIView+Category.h

 1 #import <UIKit/UIKit.h>
 2 
 3 IB_DESIGNABLE
 4 
 5 @interface UIView (Category)
 6 @property (nonatomic, assign)CGFloat x;
 7 @property (nonatomic, assign)CGFloat y;
 8 @property (nonatomic, assign)CGFloat width;
 9 @property (nonatomic, assign)CGFloat height;
10 @property (nonatomic, assign)CGFloat centerX;
11 @property (nonatomic, assign)CGFloat centerY;
12 @property (nonatomic, assign)CGSize size;
13 @property(nonatomic, assign) IBInspectable CGFloat borderWidth;
14 @property(nonatomic, assign) IBInspectable UIColor *borderColor;
15 @property(nonatomic, assign) IBInspectable CGFloat cornerRadius;
16 
17 /**
18  *  水平居中
19  */
20 - (void)alignHorizontal;
21 /**
22  *  垂直居中
23  */
24 - (void)alignVertical;
25 /**
26  *  判断是否显示在主窗口上面
27  *
28  *  @return 是否
29  */
30 - (BOOL)isShowOnWindow;
31 
32 - (UIViewController *)parentController;
33 @end

UIView+Category.m

  1 #import "UIView+Category.h"
  2 
  3 @implementation UIView (Category)
  4 
  5 - (void)setX:(CGFloat)x
  6 {
  7     CGRect frame = self.frame;
  8     frame.origin.x = x;
  9     self.frame = frame;
 10 }
 11 
 12 - (CGFloat)x
 13 {
 14     return self.frame.origin.x;
 15 }
 16 
 17 - (void)setY:(CGFloat)y
 18 {
 19     CGRect frame = self.frame;
 20     frame.origin.y = y;
 21     self.frame = frame;
 22 }
 23 
 24 - (CGFloat)y
 25 {
 26     return self.frame.origin.y;
 27 }
 28 
 29 - (void)setWidth:(CGFloat)width
 30 {
 31     CGRect frame = self.frame;
 32     frame.size.width = width;
 33     self.frame = frame;
 34 }
 35 
 36 - (CGFloat)width
 37 {
 38     return self.frame.size.width;
 39 }
 40 
 41 - (void)setHeight:(CGFloat)height
 42 {
 43     CGRect frame = self.frame;
 44     frame.size.height = height;
 45     self.frame= frame;
 46 }
 47 
 48 - (CGFloat)height
 49 {
 50     return self.frame.size.height;
 51 }
 52 
 53 - (void)setSize:(CGSize)size
 54 {
 55     CGRect frame = self.frame;
 56     frame.size = size;
 57     self.frame = frame;
 58 }
 59 
 60 - (CGSize)size
 61 {
 62     return self.frame.size;
 63 }
 64 
 65 - (void)setCenterX:(CGFloat)centerX
 66 {
 67     CGPoint center = self.center;
 68     center.x = centerX;
 69     self.center = center;
 70 }
 71 
 72 - (CGFloat)centerX
 73 {
 74     return self.center.x;
 75 }
 76 
 77 - (void)setCenterY:(CGFloat)centerY
 78 {
 79     CGPoint center = self.center;
 80     center.y = centerY;
 81     self.center = center;
 82 }
 83 
 84 - (CGFloat)centerY
 85 {
 86     return self.center.y;
 87 }
 88 - (void)alignHorizontal
 89 {
 90     self.x = (self.superview.width - self.width) * 0.5;
 91 }
 92 
 93 - (void)alignVertical
 94 {
 95     self.y = (self.superview.height - self.height) *0.5;
 96 }
 97 
 98 - (void)setBorderWidth:(CGFloat)borderWidth
 99 {
100     
101     if (borderWidth < 0) {
102         return;
103     }
104     self.layer.borderWidth = borderWidth;
105 }
106 
107 - (void)setBorderColor:(UIColor *)borderColor
108 {
109     self.layer.borderColor = borderColor.CGColor;
110 }
111 
112 - (void)setCornerRadius:(CGFloat)cornerRadius
113 {
114     self.layer.cornerRadius = cornerRadius;
115     self.layer.masksToBounds = YES;
116 }
117 
118 - (BOOL)isShowOnWindow
119 {
120     //主窗口
121     UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
122 
123     //相对于父控件转换之后的rect
124     CGRect newRect = [keyWindow convertRect:self.frame fromView:self.superview];
125     //主窗口的bounds
126     CGRect winBounds = keyWindow.bounds;
127     //判断两个坐标系是否有交汇的地方,返回bool值
128     BOOL isIntersects =  CGRectIntersectsRect(newRect, winBounds);
129     if (self.hidden != YES && self.alpha >0.01 && self.window == keyWindow && isIntersects) {
130         return YES;
131     }else{
132         return NO;
133     }
134 }
135 
136 - (CGFloat)borderWidth
137 {
138     return self.borderWidth;
139 }
140 
141 - (UIColor *)borderColor
142 {
143     return self.borderColor;
144 
145 }
146 
147 - (CGFloat)cornerRadius
148 {
149     return self.cornerRadius;
150 }
151 
152 - (UIViewController *)parentController
153 {
154     UIResponder *responder = [self nextResponder];
155     while (responder) {
156         if ([responder isKindOfClass:[UIViewController class]]) {
157             return (UIViewController *)responder;
158         }
159         responder = [responder nextResponder];
160     }
161     return nil;
162 }
163 
164 
165 @end

 

posted @ 2016-08-10 21:43  XuDeHong  阅读(2248)  评论(0编辑  收藏  举报