-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathBubblePlayView.m
210 lines (176 loc) · 7.33 KB
/
BubblePlayView.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
//
// BubblePlayView.m
// audiodemo
//
// Created by sumeng on 7/30/15.
// Copyright (c) 2015 sumeng. All rights reserved.
//
#import "BubblePlayView.h"
#define kBubblePlayViewMinWidth (kScreen_Width*0.23)
#define kBubblePlayViewMidWidth (kScreen_Width*0.35)
#define kBubblePlayViewMaxWidth (kScreen_Width*0.6)
@interface BubblePlayView ()
@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) UIImageView *playImageView;
@property (nonatomic, strong) UILabel *durationLbl;
@property (nonatomic, strong) UIActivityIndicatorView *activityView;
@property (nonatomic, strong) UIView *unreadView;
@end
@implementation BubblePlayView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_bgImageView = [[UIImageView alloc] initWithFrame:self.bounds];
_bgImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:_bgImageView];
_playImageView = [[UIImageView alloc] init];
[self addSubview:_playImageView];
_durationLbl = [[UILabel alloc] init];
_durationLbl.backgroundColor = [UIColor clearColor];
_durationLbl.font = [UIFont systemFontOfSize:16];
_durationLbl.textColor = [UIColor blackColor];
[self addSubview:_durationLbl];
_activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_activityView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
_activityView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
_activityView.hidesWhenStopped = YES;
[_activityView stopAnimating];
[self addSubview:_activityView];
_unreadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 7, 7)];
_unreadView.backgroundColor = [UIColor colorWithRGBHex:0xf75288];
_unreadView.layer.cornerRadius = _unreadView.frame.size.width/2;
_unreadView.hidden = YES;
[self addSubview:_unreadView];
self.showBgImg = YES;
self.isUnread = NO;
self.type = BubbleTypeLeft;
self.duration = 0;
}
return self;
}
- (void)sizeToFit {
[super sizeToFit];
if (_type == BubbleTypeRight) {
[_playImageView setOrigin:CGPointMake(16, (self.frame.size.height-_playImageView.frame.size.height)/2-1)];
[_durationLbl setOrigin:CGPointMake(16+8+_playImageView.frame.size.height, (self.frame.size.height-_durationLbl.frame.size.height)/2-1)];
_unreadView.center = CGPointMake(-5, self.frame.size.height/2-1);
}
else {
[_playImageView setOrigin:CGPointMake(self.frame.size.width-_playImageView.frame.size.width-16, (self.frame.size.height-_playImageView.frame.size.height)/2-1)];
[_durationLbl setOrigin:CGPointMake(self.frame.size.width-_playImageView.frame.size.width-16-8-_durationLbl.frame.size.width, (self.frame.size.height-_durationLbl.frame.size.height)/2-1)];
_unreadView.center = CGPointMake(self.frame.size.width+5, self.frame.size.height/2-1);
}
}
- (void)setType:(BubbleType)type {
_type = type;
UIImage *bgImage = nil;
UIImage *bgHlImage = nil;
UIImage *playImage = nil;
if (type == BubbleTypeRight) {
bgImage = [UIImage imageNamed:@"messageRight_bg_img"];
bgImage = [bgImage resizableImageWithCapInsets:UIEdgeInsetsMake(18, 30, bgImage.size.height - 19, bgImage.size.width - 31)];
bgHlImage = [UIImage imageNamed:@"messageRight_bg_highlight_img"];
bgHlImage = [bgHlImage resizableImageWithCapInsets:UIEdgeInsetsMake(18, 30, bgImage.size.height - 19, bgImage.size.width - 31)];
playImage = [UIImage imageNamed:@"bubble_right_play_2"];
}
else {
bgImage = [UIImage imageNamed:@"messageLeft_bg_img"];
bgImage = [bgImage resizableImageWithCapInsets:UIEdgeInsetsMake(18, 30, bgImage.size.height - 19, bgImage.size.width - 31)];
bgHlImage = [UIImage imageNamed:@"messageLeft_bg_highlight_img"];
bgHlImage = [bgHlImage resizableImageWithCapInsets:UIEdgeInsetsMake(18, 30, bgImage.size.height - 19, bgImage.size.width - 31)];
playImage = [UIImage imageNamed:@"bubble_left_play_2"];
}
_bgImageView.image = bgImage;
_bgImageView.highlightedImage = bgHlImage;
_playImageView.size = playImage.size;
_playImageView.image = playImage;
//refresh play state
self.playState = self.playState;
[self sizeToFit];
}
- (void)setDuration:(NSTimeInterval)duration {
_duration = duration;
if (duration < 0) {
_duration = 0;
}
_durationLbl.text = [NSString stringWithFormat:@"%d\"", (int)_duration];
[_durationLbl sizeToFit];
[self setWidth:[[self class] widthForDuration:_duration]];
[self sizeToFit];
}
- (void)setShowBgImg:(BOOL)showBgImg {
_showBgImg = showBgImg;
_bgImageView.hidden = !showBgImg;
}
- (void)setIsUnread:(BOOL)isUnread {
_isUnread = isUnread;
_unreadView.hidden = !isUnread;
}
- (void)setPlayState:(AudioPlayViewState)playState {
[super setPlayState:playState];
if (playState == AudioPlayViewStatePlaying) {
[_activityView stopAnimating];
[self startPlayingAnimation];
}
else if (playState == AudioPlayViewStateDownloading) {
// [_activityView startAnimating];
[self stopPlayingAnimation];
}
else {
[_activityView stopAnimating];
[self stopPlayingAnimation];
}
}
#pragma mark - Animation
- (void)startPlayingAnimation {
_playImageView.image = nil;
if (_type == BubbleTypeRight) {
_playImageView.image = [UIImage animatedImageNamed:@"bubble_right_play_" duration:0.8];
}
else {
_playImageView.image = [UIImage animatedImageNamed:@"bubble_left_play_" duration:0.8];
}
[_playImageView startAnimating];
}
- (void)stopPlayingAnimation {
[_playImageView stopAnimating];
if (_type == BubbleTypeRight) {
_playImageView.image = [UIImage imageNamed:@"bubble_right_play_2"];
}
else {
_playImageView.image = [UIImage imageNamed:@"bubble_left_play_2"];
}
}
#pragma mark - touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
_bgImageView.highlighted = YES;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
//在TableViewCell中,快速点击不显示highlight状态
[self performSelector:@selector(cancelHighlight) withObject:nil afterDelay:0.1f];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
_bgImageView.highlighted = NO;
}
- (void)cancelHighlight {
_bgImageView.highlighted = NO;
}
#pragma mark - Width
+ (CGFloat)widthForDuration:(NSTimeInterval)duration {
if (duration < 0) {
duration = 0;
}
else if (duration > 60.0f) {
duration = 60.0f;
}
if (duration <= 10.0f) {
return kBubblePlayViewMinWidth + (kBubblePlayViewMidWidth - kBubblePlayViewMinWidth) * (duration / 10);
}
else {
return kBubblePlayViewMidWidth + (kBubblePlayViewMaxWidth - kBubblePlayViewMidWidth) * ((duration - 10) / 50);
}
}
@end