-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIView+Ext.m
260 lines (223 loc) · 5.12 KB
/
UIView+Ext.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
249
250
251
252
253
254
255
256
257
258
259
260
//
// UIView+Ext.m
//
//
// Created by mokai on 14-7-30.
// Copyright (c) 2014years mokai. All rights reserved.
//
#import "UIView+Ext.h"
/**
* 视图坐标相关
*/
@implementation UIView (Position)
#pragma mark frame相关
- (CGPoint)origin {
return self.frame.origin;
}
- (void)setOrigin:(CGPoint)origin {
CGRect frame = self.frame;
frame.origin = origin;
self.frame = frame;
}
- (CGSize)size {
return self.frame.size;
}
- (void)setSize:(CGSize)size {
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
-(void)setLeft:(CGFloat)x{
CGRect rect = self.frame;
rect.origin.x = x;
self.frame = rect;
}
-(void)setTop:(CGFloat)y{
CGRect rect = self.frame;
rect.origin.y = y;
self.frame = rect;
}
-(CGFloat)left{
return self.frame.origin.x;
}
-(CGFloat)top{
return self.frame.origin.y;
}
- (CGFloat)right {
return self.frame.origin.x + self.width;
}
- (void)setRight:(CGFloat)right {
CGRect frame = self.frame;
frame.origin.x = right - frame.size.width;
self.frame = frame;
}
- (CGFloat)bottom {
return self.frame.origin.y + self.frame.size.height;
}
- (void)setBottom:(CGFloat)bottom {
CGRect frame = self.frame;
frame.origin.y = bottom - frame.size.height;
self.frame = frame;
}
-(CGFloat)width{
return self.frame.size.width;
}
-(CGFloat)height{
return self.frame.size.height;
}
-(void)setWidth:(CGFloat)width{
CGRect rect = self.frame;
rect.size.width = width;
self.frame = rect;
}
-(void)setHeight:(CGFloat)height{
CGRect rect = self.frame;
rect.size.height = height;
self.frame = rect;
}
/**
* 距离屏幕的left
*
*/
- (CGFloat)screenLeft {
CGFloat x = 0;
for (UIView *view = self; view; view = view.superview){
x += view.left;
if ([view isKindOfClass:[UIScrollView class]]){
UIScrollView *scrollView = (UIScrollView*)view;
x -= scrollView.contentOffset.x;
}
}
return x;
}
/**
* 距离屏幕的top
*/
- (CGFloat)screenTop {
CGFloat y = 0;
for (UIView *view = self; view; view = view.superview){
y += view.top;
if ([view isKindOfClass:[UIScrollView class]]){
UIScrollView *scrollView = (UIScrollView*)view;
y -= scrollView.contentOffset.y;
}
}
return y - [UIApplication sharedApplication].statusBarFrame.size.height;
}
- (CGRect)screenFrame {
return CGRectMake(self.screenLeft, self.screenTop, self.width, self.height);
}
#pragma mark 视图居中相关
/**
* 根据传入的子视图与当前视图计算出水平中心开始点
*/
-(CGFloat)centerHorizontalWithSubview:(UIView *)subview{
return self.width/2 - subview.width/2;
}
/**
* 根据传入的子视图与当前视图计算出垂直中心开始点
*/
-(CGFloat)centerVerticalWithSubview:(UIView *)subview{
return self.height/2 - subview.height/2;
}
/**
* 根据传入的子视图计算出中心开始点
**/
-(CGPoint)centerWithSubview:(UIView *)subview{
return CGPointMake([self centerHorizontalWithSubview:subview],[self centerVerticalWithSubview:subview]);
}
/**
* 居中增加子视图
*/
-(void)addSubviewToCenter:(UIView *)subview{
CGRect rect = subview.frame;
rect.origin = [self centerWithSubview:subview];
subview.frame = rect;
[self addSubview:subview];
}
-(void)addSubviewToHorizontalCenter:(UIView *)subview{
CGRect rect = subview.frame;
rect.origin.x = [self centerHorizontalWithSubview:subview];
subview.frame = rect;
[self addSubview:subview];
}
-(void)addSubviewToVerticalCenter:(UIView *)subview{
CGRect rect = subview.frame;
rect.origin.y = [self centerVerticalWithSubview:subview];
subview.frame = rect;
[self addSubview:subview];
}
@end
/**
* 视图层次相关
*/
#pragma mark 视图层次相关
@implementation UIView (ZOrder)
/**
* 当前视图在父视图中的位置
*/
-(int)getSubviewIndex
{
return [self.superview.subviews indexOfObject:self];
}
/**
* 将视图置于父视图最上面
*/
-(void)bringToFront
{
[self.superview bringSubviewToFront:self];
}
/**
* 将视图置于父视图最下面
*/
-(void)sendToBack
{
[self.superview sendSubviewToBack:self];
}
/**
* 视图层次上移一层
*/
-(void)bringOneLevelUp
{
int currentIndex = [self getSubviewIndex];
[self.superview exchangeSubviewAtIndex:currentIndex withSubviewAtIndex:currentIndex+1];
}
/**
* 视图层次下移一层
*/
-(void)sendOneLevelDown
{
int currentIndex = [self getSubviewIndex];
[self.superview exchangeSubviewAtIndex:currentIndex withSubviewAtIndex:currentIndex-1];
}
/**
* 是否在最上面
*/
-(BOOL)isInFront
{
return ([self.superview.subviews lastObject]==self);
}
/**
* 是否在最下面
*/
-(BOOL)isAtBack
{
return ([self.superview.subviews objectAtIndex:0]==self);
}
/**
* 交换层次
*/
-(void)swapDepthsWithView:(UIView*)swapView
{
[self.superview exchangeSubviewAtIndex:[self getSubviewIndex] withSubviewAtIndex:[swapView getSubviewIndex]];
}
/**
* 清空所有子视图
*/
-(void)removeAllSubview{
for(UIView *view in [self subviews])
{
[view removeFromSuperview];
}
}
@end