CNLiveMineHeaderView.m
13.1 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//
// CNLiveMineHeaderView.m
// CNLiveMineModule
//
// Created by 153993236@qq.com on 11/12/2019.
// Copyright © 2019 153993236@qq.com. All rights reserved.
//
#import "CNLiveMineHeaderView.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>
#import "QMUIKit.h"
#import "CNLiveCategory.h"
#import "CNLiveBaseKit.h"
#import "CNUserInfoManager.h"
#import "CNLiveUserInfoNotification.h"
@interface CNLiveMineHeaderView ()
@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) UIButton *witness;
@property (nonatomic, strong) UIImageView *header;
@property (nonatomic, strong) UILabel *name;
@property (nonatomic, strong) UILabel *certification;
@property (nonatomic, strong) UILabel *address;
@property (nonatomic, strong) UIButton *sign;
@end
@implementation CNLiveMineHeaderView
static const NSInteger margin = 20;
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
self.userInteractionEnabled = YES;
self.backgroundColor = [UIColor colorWithRed:244.0/255.0 green:244.0/255.0 blue:244.0/255.0 alpha:1.0];
[CNLiveUserInfoNotification addUserInfoUpdateObserver:self selector:@selector(userInfoUpdateNotification:)];
[self createSubViews];
}
return self;
}
// 实现方法
- (void)userInfoUpdateNotification:(NSNotification *)noti{
//使用userInfo处理消息
NSDictionary *dic = [noti userInfo];
CNLiveUserInfoDataType type = [[dic objectForKey:@"dataType"] integerValue];
NSString *info = [dic objectForKey:@"info"];
if(type == CNLiveUserInfoDataTypeHeader){
UIImage *xw_image = [self getImageWithImageName:@"default_avatar_y" bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineHeaderView class]];
[_header sd_setImageWithURL:[NSURL URLWithString:info] placeholderImage:xw_image];
}else if(type == CNLiveUserInfoDataTypeNickname){
_name.attributedText = [self createAttributedImageName:@"mine_code" string:info font:16.0 isFront:NO];
}else if(type == CNLiveUserInfoDataTypeAddress){
_address.attributedText = [self createAttributedImageName:@"mine_location" string:info font:13.0 isFront:YES];
}
}
- (void)dealloc {
NSLog(@"CNLiveMineHeaderView -- dealloc");
[CNLiveUserInfoNotification removeObserver:self];
}
- (void)createSubViews {
[self addSubview:self.bgView];
[self addSubview:self.witness];
[self addSubview:self.header];
[self addSubview:self.name];
[self addSubview:self.certification];
[self addSubview:self.address];
[self addSubview:self.sign];
}
- (void)layoutSubviews{
[super layoutSubviews];
[_witness mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mas_left).with.offset(1.5*margin);
make.right.equalTo(self.mas_right).with.offset(-1.5*margin);
make.width.height.offset(60);
make.bottom.equalTo(self.mas_bottom).with.offset(-0.5*margin);
}];
[_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mas_left).with.offset(0);
make.right.equalTo(self.mas_right).with.offset(-0);
make.top.equalTo(self.mas_top).with.offset(0);
make.bottom.equalTo(self.witness.mas_centerY).with.offset(0.5*margin);
}];
[_header mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mas_left).with.offset(margin);
make.centerY.equalTo(self.bgView.mas_centerY).with.offset(0);
make.width.height.offset(60);
}];
[_certification mas_remakeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.header.mas_centerY);
make.left.equalTo(self.header.mas_right).with.offset(margin/2.0);
make.height.offset(35);
}];
[_name mas_remakeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.certification.mas_top);
make.left.equalTo(self.certification.mas_left);
make.right.mas_lessThanOrEqualTo(self.bgView.mas_right).with.offset(-margin);
make.height.equalTo(self.certification.mas_height);
}];
[_address mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.certification.mas_bottom);
make.left.equalTo(self.name.mas_left);
make.right.equalTo(self.name.mas_right);
make.height.equalTo(self.name.mas_height);
}];
[_sign mas_remakeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.mas_right).with.offset(0);
make.centerY.equalTo(self.header.mas_centerY).with.offset(0);
make.width.mas_equalTo(105);
make.height.mas_equalTo(32);
}];
}
- (void)setInteractionEnabled:(BOOL)interactionEnabled{
_interactionEnabled = interactionEnabled;
_sign.userInteractionEnabled = interactionEnabled;
_witness.userInteractionEnabled = interactionEnabled;
}
- (void)setIsRefresh:(BOOL)isRefresh{
_isRefresh = isRefresh;
if(isRefresh){
_name.attributedText = [self createAttributedImageName:@"mine_code" string:CNUserShareModel.nickname font:16.0 isFront:NO];
_address.attributedText = [self createAttributedImageName:@"mine_location" string:CNUserShareModel.location font:13.0 isFront:YES];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
#pragma mark - Private Methods
- (NSMutableAttributedString *)createAttributedImageName:(NSString *)imageName string:(NSString *)string font:(CGFloat)font isFront:(BOOL)isFront{
if(isFront){
string = [NSString stringWithFormat:@" %@",string];
}
// 1.创建一个富文本
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:string];
// 修改富文本中的不同文字的样式
// [attri addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, string.length)];
[attri addAttribute:NSFontAttributeName value:UIFontCNMake(font) range:NSMakeRange(0, string.length)];
// 2.添加表情图片
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
// 表情图片
UIImage *xw_image = [self getImageWithImageName:imageName bundleName:@"CNLiveMineModule" targetClass:[self class]];
attch.image = xw_image;
if(isFront){
// 设置图片大小
attch.bounds = CGRectMake(0, -2, 10, 12);
// 创建带有图片的富文本
NSAttributedString *str = [NSAttributedString attributedStringWithAttachment:attch];
[attri insertAttributedString:str atIndex:0];// 插入某个位置
}else{
// 设置图片大小
attch.bounds = CGRectMake(10, -2, 15, 15);
// 创建带有图片的富文本
NSAttributedString *str = [NSAttributedString attributedStringWithAttachment:attch];
[attri insertAttributedString:str atIndex:string.length];// 插入某个位置
}
return attri;
}
- (NSMutableAttributedString *)createAttributedImages:(NSArray *)images {
// 1.创建一个富文本
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@""];
for(NSString *image in images){
// 2.添加表情图片
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
// 表情图片
UIImage *xw_image = [self getImageWithImageName:image bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineHeaderView class]];
attch.image = xw_image;
// 创建带有图片的富文本
NSAttributedString *str = [NSAttributedString attributedStringWithAttachment:attch];
NSAttributedString *space = [[NSAttributedString alloc]initWithString:@" "];
[attri insertAttributedString:space atIndex:0];
[attri insertAttributedString:str atIndex:0];// 插入某个位置
}
return attri;
}
#pragma mark - 响应方法
- (void)headerTapDidClicked:(UITapGestureRecognizer *)tap{
if (self.delegate && [self.delegate respondsToSelector:@selector(headerView:pushToController:)]) {
[self.delegate headerView:self pushToController:CNLiveMineHeaderViewTypeUerInfo];
}
}
- (void)nameTapDidClicked:(UITapGestureRecognizer *)tap{
if (self.delegate && [self.delegate respondsToSelector:@selector(headerView:pushToController:)]) {
[self.delegate headerView:self pushToController:CNLiveMineHeaderViewTypeCode];
}
}
- (void)signDidClicked:(UIButton *)btn{
if (self.delegate && [self.delegate respondsToSelector:@selector(headerView:pushToController:)]) {
btn.userInteractionEnabled = NO;
[self.delegate headerView:self pushToController:CNLiveMineHeaderViewTypeSign];
}
}
- (void)witnessDidClicked:(UIButton *)btn{
if (self.delegate && [self.delegate respondsToSelector:@selector(headerView:pushToController:)]) {
btn.userInteractionEnabled = NO;
[self.delegate headerView:self pushToController:CNLiveMineHeaderViewTypeWintess];
}
}
#pragma mark - 懒加载
- (UIButton *)witness {
if (!_witness) {
_witness = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *xw_image = [self getImageWithImageName:@"mine_observer" bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineHeaderView class]];
_witness.imageView.contentMode = UIViewContentModeScaleAspectFit;
[_witness setImage:xw_image forState:UIControlStateNormal];
_witness.adjustsImageWhenHighlighted = NO;
[_witness addTarget:self action:@selector(witnessDidClicked:) forControlEvents:UIControlEventTouchUpInside];
}
return _witness;
}
- (UIImageView *)bgView {
if (!_bgView) {
_bgView = [[UIImageView alloc] init];
_bgView.userInteractionEnabled = YES;
UIImage *xw_image = [self getImageWithImageName:@"mine_bg" bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineHeaderView class]];
_bgView.image = xw_image;
}
return _bgView;
}
- (UIImageView *)header {
if (!_header) {
_header = [[UIImageView alloc] init];
_header.userInteractionEnabled = YES;
_header.layer.masksToBounds = YES;
_header.layer.cornerRadius = 30;
UIImage *xw_image = [self getImageWithImageName:@"default_avatar_y" bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineHeaderView class]];
[_header sd_setImageWithURL:[NSURL URLWithString:CNUserShareModel.faceUrl] placeholderImage:xw_image];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headerTapDidClicked:)];
[_header addGestureRecognizer:tap];
}
return _header;
}
- (UILabel *)name {
if (!_name) {
_name = [[UILabel alloc] init];
_name.userInteractionEnabled = YES;
_name.numberOfLines = 1;
_name.textColor = [UIColor whiteColor];
_name.font = [UIFont systemFontOfSize:16];
_name.attributedText = [self createAttributedImageName:@"mine_code" string:CNUserShareModel.nickname font:16.0 isFront:NO];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(nameTapDidClicked:)];
[_name addGestureRecognizer:tap];
}
return _name;
}
- (UILabel *)certification {
if (!_certification) {
_certification = [[UILabel alloc] init];
_certification.numberOfLines = 1;
_certification.textColor = [UIColor whiteColor];
_certification.font = [UIFont systemFontOfSize:16];
_certification.attributedText = [self createAttributedImages:@[@"mine_overbadge",@"mine_common", @"mine_wjj_certification"]];
}
return _certification;
}
- (UILabel *)address {
if (!_address) {
_address = [[UILabel alloc] init];
_address.numberOfLines = 1;
_address.textColor = [UIColor colorWithRed:140/255.0 green:140/255.0 blue:142/255.0 alpha:1.0];
_address.font = [UIFont systemFontOfSize:13];
_address.attributedText = [self createAttributedImageName:@"mine_location" string:CNUserShareModel.location font:13.0 isFront:YES];
}
return _address;
}
- (UIButton *)sign {
if (!_sign) {
_sign = [UIButton buttonWithType:UIButtonTypeCustom];
_sign.frame = CGRectMake(0, 0, 105, 32);
_sign.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.2];
[_sign setTitle:@"签到赚积分" forState:UIControlStateNormal];
_sign.titleLabel.font = [UIFont systemFontOfSize:13.0];
[_sign setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
UIImage *xw_image = [self getImageWithImageName:@"sign_in_soccer" bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineHeaderView class]];
[_sign setImage:xw_image forState:UIControlStateNormal];
[_sign addTarget:self action:@selector(signDidClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_sign.layer.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft cornerRadii:CGSizeMake(16, 16)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = _sign.bounds;
maskLayer.path = maskPath.CGPath;
_sign.layer.mask = maskLayer;
}
return _sign;
}
@end