GrounderView.m
3.77 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
//
// GrounderView.m
// GrounderDemo
//
// Created by 贾楠 on 16/3/8.
// Copyright © 2016年 贾楠. All rights reserved.
//
#import "GrounderView.h"
#import "IVTBarrageModel.h"
@interface GrounderView()
{
float viewWidth;
}
@end
@implementation GrounderView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.cornerRadius = 30/2;
self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
_titleLabel = [[UILabel alloc] init];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.font = [UIFont boldSystemFontOfSize:12];
[self addSubview:_titleLabel];
_nameLabel = [[UILabel alloc] init];
_nameLabel.textColor = [UIColor IVTLightRedColor];
_nameLabel.font = [UIFont systemFontOfSize:10];
[self addSubview:_nameLabel];
_headImage = [[UIImageView alloc] init];
_headImage.clipsToBounds = YES;
_headImage.frame = CGRectMake(0, 0, 30, 30);
_headImage.layer.cornerRadius = 30/2;
_headImage.layer.borderWidth = 0.5;
_headImage.layer.borderColor = [UIColor whiteColor].CGColor;
[self addSubview:_headImage];
}
return self;
}
- (void)setContent:(id)model{
IVTBarrageModel *barrageModel = (IVTBarrageModel* )model;
_nameLabel.text = [barrageModel.title base64DecodedToString];
_nameLabel.frame = CGRectMake(35, 2, [GrounderView calculateMsgWidth:_nameLabel.text andWithLabelFont:[UIFont systemFontOfSize:10] andWithHeight:10], 10);
[_headImage sd_setImageWithURL:[NSURL URLWithString:barrageModel.headImageStr] placeholderImage:[UIImage imageNamed:@"占位头像"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
_headImage.alpha = 0.0;
[UIView transitionWithView:_headImage
duration:1.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[_headImage setImage:image];
_headImage.alpha = 1.0;
} completion:NULL];
}];
_titleLabel.text = barrageModel.name;
_titleLabel.frame = CGRectMake(35, 12, [GrounderView calculateMsgWidth:_titleLabel.text andWithLabelFont:[UIFont systemFontOfSize:12] andWithHeight:18], 18);
viewWidth = _titleLabel.frame.size.width + 55;
if (_nameLabel.frame.size.width > _titleLabel.frame.size.width) {
viewWidth = _nameLabel.frame.size.width + 55;
}
self.frame = CGRectMake(kScreenWidth + 20, self.selfYposition, viewWidth, 30);
}
- (void)grounderAnimation:(id)model{
float second = 0.0;
if (_titleLabel.text.length < 30){
second = 10.0f;
}else{
second = _titleLabel.text.length/2.5;
}
[UIView animateWithDuration:second animations:^{
self.frame = CGRectMake( - viewWidth - 20, self.frame.origin.y, viewWidth, 30);
}completion:^(BOOL finished) {
[self removeFromSuperview];
self.isShow = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:@"nextView" object:nil];
}];
}
+ (CGFloat)calculateMsgWidth:(NSString *)msg andWithLabelFont:(UIFont*)font andWithHeight:(NSInteger)height {
if ([msg isEqualToString:@""]) {
return 0;
}
CGFloat messageLableWidth = [msg boundingRectWithSize:CGSizeMake(MAXFLOAT, height)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:font}
context:nil].size.width;
return messageLableWidth + 1;
}
@end