-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIView+GFExtension.m
248 lines (174 loc) · 4.13 KB
/
UIView+GFExtension.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//
// UIView+Extension.m
// CaoCaoKD
//
// Created by goofygao on 16/2/25.
// Copyright © 2016年 goofyy. All rights reserved.
//
#import "UIView+GFExtension.h"
@implementation UIView (GFExtension)
/**
* 添加边框:注给scrollView添加会出错
*
* @param direct 方向
* @param color 颜色
* @param width 线宽
*/
-(void)addSingleBorder:(UIViewBorderDirect)direct color:(UIColor *)color width:(CGFloat)width{
UIView *line=[[UIView alloc] init];
//设置颜色
line.backgroundColor=color;
//添加
[self addSubview:line];
//禁用ar
line.translatesAutoresizingMaskIntoConstraints=NO;
NSDictionary *views=NSDictionaryOfVariableBindings(line);
NSDictionary *metrics=@{@"w":@(width),@"y":@(self.height - width),@"x":@(self.width - width)};
NSString *vfl_H=@"";
NSString *vfl_W=@"";
//上
if(UIViewBorderDirectTop==direct){
vfl_H=@"H:|-0-[line]-0-|";
vfl_W=@"V:|-0-[line(==w)]";
}
//左
if(UIViewBorderDirectLeft==direct){
vfl_H=@"H:|-0-[line(==w)]";
vfl_W=@"V:|-0-[line]-0-|";
}
//下
if(UIViewBorderDirectBottom==direct){
vfl_H=@"H:|-0-[line]-0-|";
vfl_W=@"V:[line(==w)]-0-|";
}
//右
if(UIViewBorderDirectRight==direct){
vfl_H=@"H:[line(==w)]-0-|";
vfl_W=@"V:|-0-[line]-0-|";
}
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl_H options:0 metrics:metrics views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl_W options:0 metrics:metrics views:views]];
}
/**
* 自动从xib创建视图
*/
+(instancetype)viewFromXIB{
NSString *name=NSStringFromClass(self);
UIView *xibView=[[[NSBundle mainBundle] loadNibNamed:name owner:nil options:nil] firstObject];
if(xibView==nil){
NSLog(@"CoreXibView:从xib创建视图失败,当前类是:%@",name);
}
return xibView;
}
- (void)setX:(CGFloat)x
{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
- (void)setY:(CGFloat)y
{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
- (CGFloat)x
{
return self.frame.origin.x;
}
- (CGFloat)y
{
return self.frame.origin.y;
}
- (void)setWidth:(CGFloat)width
{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
- (void)setHeight:(CGFloat)height
{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
- (CGFloat)height
{
return self.frame.size.height;
}
- (CGFloat)width
{
return self.frame.size.width;
}
- (void)setSize:(CGSize)size
{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
- (CGSize)size
{
return self.frame.size;
}
- (void)setOrigin:(CGPoint)origin
{
CGRect frame = self.frame;
frame.origin = origin;
self.frame = frame;
}
- (CGPoint)origin
{
return self.frame.origin;
}
#pragma mark 添加一组子view:
-(void)addSubviewsWithArray:(NSArray *)subViews{
for (UIView *view in subViews) {
[self addSubview:view];
}
}
#pragma mark 圆角处理
-(void)setRadius:(CGFloat)r{
if(r<=0) r=self.frame.size.width * .5f;
//圆角半径
self.layer.cornerRadius=r;
//强制
self.layer.masksToBounds=YES;
}
-(CGFloat)radius{
return 0;
}
/**
* 添加底部的边线
*/
-(void)setBottomBorderColor:(UIColor *)bottomBorderColor{
}
-(UIColor *)bottomBorderColor{
return nil;
}
/**
* 添加边框
*/
-(void)setBorder:(UIColor *)color width:(CGFloat)width{
CALayer *layer=self.layer;
layer.borderColor=color.CGColor;
layer.borderWidth=width;
}
/**
* 调试
*/
-(void)debug:(UIColor *)color width:(CGFloat)width{
[self setBorder:color width:width];
}
/**
* 批量移除视图
*
* @param views 需要移除的视图数组
*/
+(void)removeViews:(NSArray *)views{
dispatch_async(dispatch_get_main_queue(), ^{
for (UIView *view in views) {
[view removeFromSuperview];
}
});
}
@end