This repository was archived by the owner on Jul 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodePressAnimationView.m
More file actions
240 lines (164 loc) · 7.2 KB
/
NodePressAnimationView.m
File metadata and controls
240 lines (164 loc) · 7.2 KB
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
//
// NodePressAnimationView.m
// YoCelsius
//
// Created by YouXianMing on 15/11/12.
// Copyright © 2015年 XianMingYou. All rights reserved.
//
#import "NodePressAnimationView.h"
#define SHOW_VIEW_WIDTH 1000
#define TIME_END_DURATION 0.5
#define TIME_NOR_DURATION 0.35
@interface NodePressAnimationView ()
@property (nonatomic, strong) UIButton *button; // 创建出按钮
@property (nonatomic, strong) UILabel *normalLabel; // 普通label
@property (nonatomic, strong) UILabel *highlightLabel; // 高亮状态的label
@property (nonatomic, strong) UIView *showView; // 显示用的view
@end
@implementation NodePressAnimationView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.layer.masksToBounds = YES;
// 创建出showView
[self createShowView];
// 创建出button
[self createButtonWithFrame:self.bounds];
// 创建出label
[self createLabelsWithFrame:self.bounds];
}
return self;
}
#pragma mark - button相关
- (void)createButtonWithFrame:(CGRect)frame {
self.button = [[UIButton alloc] initWithFrame:frame];
// 添加按钮
[self addSubview:self.button];
// 添加事件
{
// 按住按钮后没有松手的动画
[self.button addTarget:self
action:@selector(buttonTouchDownAndDragEnter)
forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
// 按住按钮松手后的动画
[self.button addTarget:self
action:@selector(buttonTouchUpInside)
forControlEvents:UIControlEventTouchUpInside];
// 按住按钮后拖拽出去的动画
[self.button addTarget:self
action:@selector(buttonTouchDragExit)
forControlEvents:UIControlEventTouchDragExit];
}
}
- (void)buttonTouchDownAndDragEnter {
self.showView.bounds = ((CALayer *)self.showView.layer.presentationLayer).bounds;
self.showView.alpha = ((CALayer *)self.showView.layer.presentationLayer).opacity;
self.normalLabel.alpha = ((CALayer *)self.normalLabel.layer.presentationLayer).opacity;
self.highlightLabel.alpha = ((CALayer *)self.highlightLabel.layer.presentationLayer).opacity;
// 移除之前的动画状态
[self.showView.layer removeAllAnimations];
[UIView animateWithDuration:(self.toEndDuration <= 0 ? TIME_END_DURATION : self.toEndDuration) animations:^{
self.showView.bounds = CGRectMake(0, 0, SHOW_VIEW_WIDTH, (self.animationWidth <= 0? SHOW_VIEW_WIDTH : self.animationWidth));
self.showView.alpha = 1;
self.normalLabel.alpha = 0.f;
self.highlightLabel.alpha = 1.f;
} completion:^(BOOL finished) {
if (finished == YES) {
if (_delegate && ([_delegate respondsToSelector:@selector(nodePressAnimationViewCompleteEventWithView:)])) {
[_delegate nodePressAnimationViewCompleteEventWithView:self];
}
}
}];
}
- (void)buttonTouchUpInside {
self.showView.bounds = ((CALayer *)self.showView.layer.presentationLayer).bounds;
self.showView.alpha = ((CALayer *)self.showView.layer.presentationLayer).opacity;
self.normalLabel.alpha = ((CALayer *)self.normalLabel.layer.presentationLayer).opacity;
self.highlightLabel.alpha = ((CALayer *)self.highlightLabel.layer.presentationLayer).opacity;
// 移除之前的动画状态
[self.showView.layer removeAllAnimations];
[UIView animateWithDuration:(self.toNormalDuration <= 0 ? TIME_NOR_DURATION : self.toNormalDuration) animations:^{
self.showView.bounds = CGRectMake(0, 0, SHOW_VIEW_WIDTH, 0);
self.showView.alpha = 0;
self.normalLabel.alpha = 1.f;
self.highlightLabel.alpha = 0.f;
} completion:nil];
}
- (void)buttonTouchDragExit {
self.showView.bounds = ((CALayer *)self.showView.layer.presentationLayer).bounds;
self.showView.alpha = ((CALayer *)self.showView.layer.presentationLayer).opacity;
self.normalLabel.alpha = ((CALayer *)self.normalLabel.layer.presentationLayer).opacity;
self.highlightLabel.alpha = ((CALayer *)self.highlightLabel.layer.presentationLayer).opacity;
// 移除之前的动画状态
[self.showView.layer removeAllAnimations];
[UIView animateWithDuration:(self.toNormalDuration <= 0 ? TIME_NOR_DURATION : self.toNormalDuration) animations:^{
self.showView.bounds = CGRectMake(0, 0, SHOW_VIEW_WIDTH, 0);
self.showView.alpha = 0;
self.normalLabel.alpha = 1.f;
self.highlightLabel.alpha = 0.f;
} completion:nil];
}
#pragma mark - showView相关
- (void)createShowView {
self.showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SHOW_VIEW_WIDTH, 0)];
[self addSubview:self.showView];
self.showView.alpha = 0.f;
self.showView.center = CGPointMake(self.frame.size.width / 2.f, self.frame.size.height / 2.f);
self.showView.transform = CGAffineTransformMakeRotation(45 * M_PI / 180.0);
self.showView.backgroundColor = [UIColor redColor];
self.showView.userInteractionEnabled = NO;
}
#pragma mark - label相关
- (void)createLabelsWithFrame:(CGRect)frame {
self.normalLabel = [[UILabel alloc] initWithFrame:frame];
self.highlightLabel = [[UILabel alloc] initWithFrame:frame];
self.normalLabel.alpha = 1.f;
self.highlightLabel.alpha = 0.f;
self.normalLabel.textAlignment = NSTextAlignmentCenter;
self.highlightLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.highlightLabel];
[self addSubview:self.normalLabel];
}
#pragma mark - 重写setter,getter方法
@synthesize font = _font;
- (void)setFont:(UIFont *)font {
_font = font;
self.normalLabel.font = font;
self.highlightLabel.font = font;
}
- (UIFont *)font {
return _font;
}
@synthesize text = _text;
- (void)setText:(NSString *)text {
_text = text;
self.normalLabel.text = text;
self.highlightLabel.text = text;
}
- (NSString *)text {
return _text;
}
@synthesize normalTextColor = _normalTextColor;
- (void)setNormalTextColor:(UIColor *)normalTextColor {
_normalTextColor = normalTextColor;
self.normalLabel.textColor = normalTextColor;
}
- (UIColor *)normalTextColor {
return _normalTextColor;
}
@synthesize highlightTextColor = _highlightTextColor;
- (void)setHighlightTextColor:(UIColor *)highlightTextColor {
_highlightTextColor = highlightTextColor;
self.highlightLabel.textColor = highlightTextColor;
}
- (UIColor *)highlightTextColor {
return _highlightTextColor;
}
@synthesize animationColor = _animationColor;
- (void)setAnimationColor:(UIColor *)animationColor {
_animationColor = animationColor;
self.showView.backgroundColor = animationColor;
}
- (UIColor *)animationColor {
return _animationColor;
}
@end