JLChen
2021-04-30 a5247b61d585627a1a7b1e1f35f34de9f0af9fba
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
//
//  Copyright (c) 2015年 dahua. All rights reserved.
//
 
#import "UIView+LeChange.h"
#import <QuartzCore/CALayer.h>
 
@implementation UIView (LeChange)
 
- (CGFloat)dh_width
{
    return self.frame.size.width;
}
 
- (void)setDh_width:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}
 
- (CGFloat)dh_height
{
    return self.frame.size.height;
}
 
- (void)setDh_height:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}
 
- (CGFloat)dh_x
{
    return self.frame.origin.x;
}
 
- (void)setDh_x:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}
 
- (CGFloat)dh_y
{
    return self.frame.origin.y;
}
 
- (void)setDh_y:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}
 
- (CGSize)dh_size
{
    return self.frame.size;
}
 
- (void)setDh_size:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}
 
- (CGFloat)dh_centerX
{
    return self.center.x;
}
 
- (void)setDh_centerX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}
 
- (CGFloat)dh_centerY
{
    return self.center.y;
}
 
- (void)setDh_centerY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}
 
- (CGFloat)dh_left {
    return self.frame.origin.x;
}
 
- (void)setDh_left:(CGFloat)x {
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}
 
- (CGFloat)dh_top {
    return self.frame.origin.y;
}
 
- (void)setDh_top:(CGFloat)y {
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}
 
- (CGFloat)dh_right {
    return self.frame.origin.x + self.frame.size.width;
}
 
- (void)setDh_right:(CGFloat)right {
    CGRect frame = self.frame;
    if (right > self.frame.origin.x) {
        frame.origin.x = right - self.frame.size.width;
    } else {
        frame.origin.x = self.frame.origin.x - self.frame.size.width;
    }
    self.frame = frame;
}
 
- (CGFloat)dh_bottom {
    return self.frame.origin.y + self.frame.size.height;
}
 
- (void)setDh_bottom:(CGFloat)bottom {
    CGRect frame = self.frame;
    if (bottom > self.frame.origin.y) {
        frame.origin.y = bottom - self.frame.origin.y;
    } else {
        frame.origin.y = self.frame.origin.y - self.frame.size.height;
    }
    self.frame = frame;
}
 
- (CGFloat)dh_cornerRadius
{
    return self.layer.cornerRadius;
}
 
- (void)setDh_cornerRadius:(CGFloat)cornerRadius
{
    self.layer.cornerRadius = cornerRadius;
}
 
- (void)lc_removeAllSubview
{
    for (UIView *childView in self.subviews)
    {
        [childView removeFromSuperview];
    }
}
 
- (void)lc_addIconBtnArray:(NSArray *)btnArray
{
    NSMutableArray *tempBtnArray = [NSMutableArray new];
    for (UIView *v in btnArray) {
        if (!v.hidden) {
            [tempBtnArray addObject:v];
        }
    }
    NSInteger count = tempBtnArray.count;
    for (NSInteger i=0; i<count; i++) {
        UIView *btn = tempBtnArray[i];
        [self addSubview:btn];
        btn.dh_y = 0;
        btn.dh_centerX = self.dh_width/(count+1) * (i+1);
    }
}
 
- (void)lc_addSubviewToCeneter:(UIView *)view
{
    [self addSubview:view];
    CGPoint center = CGPointMake(self.dh_width/2, self.dh_height/2);
    view.center = center;
}
 
- (void)lc_setBoraderWith:(CGFloat )borderWidth andColor:(UIColor *)borderColor
{
    self.layer.borderWidth = borderWidth;
    
    self.layer.borderColor = [borderColor CGColor];
}
 
- (void)lc_addBorderWidth:(CGFloat)width color:(UIColor*)color radius:(CGFloat)radius {
    self.layer.masksToBounds = YES;
    self.layer.borderWidth = width;
    self.layer.cornerRadius = radius;
    self.layer.borderColor = color.CGColor == nil ? [UIColor lightGrayColor].CGColor : color.CGColor;
}
 
- (void)lc_setRound {
    [self lc_setRadius:self.frame.size.height / 2];
}
 
- (void)lc_setRadius:(CGFloat)radius {
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = radius;
}
 
- (void)lc_animationWithPath:(NSString *)keyPath transform:(CATransform3D)transform duration:(CFTimeInterval)duration delegate:(id)delegate {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:keyPath];
    NSValue *value = [NSValue valueWithCATransform3D:transform];
    [animation setToValue:value];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [animation setDuration:duration];
    animation.delegate = delegate;
    animation.cumulative = YES;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    
    [self.layer addAnimation:animation forKey:nil];
}
 
- (void)lc_shakeViewWithRepeatCount:(NSInteger)repeatCount
{
    CALayer *layer = [self layer];
    CGPoint posLayer = [layer position];
    CGPoint start = CGPointMake(posLayer.x-5, posLayer.y);
    CGPoint end = CGPointMake(posLayer.x+5, posLayer.y);
    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"position"];
    [animation setTimingFunction:[CAMediaTimingFunction
                                  functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [animation setFromValue:[NSValue valueWithCGPoint:start]];
    [animation setToValue:[NSValue valueWithCGPoint:end]];
    [animation setAutoreverses:YES];
    [animation setDuration:0.08];
    [animation setRepeatCount:repeatCount];
    [layer addAnimation:animation forKey:nil];
}
 
@end