ComDynamicCell.m
9.9 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
//
// ComDynamicCell.m
// DreamSleep
//
// Created by peter on 2022/9/27.
//
#import "ComDynamicCell.h"
#import "GKPhotoBrowser.h"
#import "LikeButton.h"
@interface ComDynamicCell ()
@property (nonatomic, assign) DynModelType modelType;
@property (nonatomic, assign) CGFloat margin;
@property (nonatomic, strong) UIImageView *userIcon;
@property (nonatomic, strong) UILabel *userNameLab;
@property (nonatomic, strong) UILabel *timeLab;
@property (nonatomic, strong) UIButton *informBtn;
@property (nonatomic, strong) UIView *imgContainerView;
@property (nonatomic, strong) UILabel *contentLab;
@property (nonatomic, strong) LikeButton *likeBtn;
@property (nonatomic, strong) UIButton *remarkCountBtn;
@property (nonatomic, strong) NSMutableArray *photos;
@end
@implementation ComDynamicCell
#pragma mark - init
- (instancetype)initWithCellType:(DynModelType)type style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.modelType = type;
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.photos = [NSMutableArray array];
[self.contentView addSubview:self.userIcon];
[self.contentView addSubview:self.userNameLab];
[self.contentView addSubview:self.timeLab];
[self.contentView addSubview:self.informBtn];
[self.contentView addSubview:self.imgContainerView];
[self.contentView addSubview:self.contentLab];
[self.contentView addSubview:self.likeBtn];
[self.contentView addSubview:self.remarkCountBtn];
self.margin = 15;
if (type == DynModelTypeCom) {
[self cornerRadius:24.0];
self.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, CornerViewDarkColor, DSWhite);
} else {
self.margin = 30;
[self hideNeedlessView];
self.backgroundColor = DSClearColor;
}
[self.userIcon mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(self.margin);
make.top.equalTo(self.contentView).offset(15);
make.size.mas_equalTo(CGSizeMake(40, 40));
}];
[self.userNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.userIcon.mas_right).offset(14);
make.top.equalTo(self.userIcon);
make.right.equalTo(self.informBtn.mas_left).offset(-14);
}];
[self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.userNameLab);
make.top.equalTo(self.contentView).offset(38);
}];
[self.informBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView).offset(15);
make.right.equalTo(self.contentView).offset(-15);
make.size.mas_equalTo(CGSizeMake(30, 30));
}];
[self.remarkCountBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.contentView).offset(-15);
make.bottom.equalTo(self.contentView).offset(-10);
make.size.mas_equalTo(CGSizeMake(100, 30));
}];
[self.likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.remarkCountBtn.mas_left);
make.size.bottom.equalTo(self.remarkCountBtn);
}];
}
return self;
}
#pragma mark - public
- (void)fireLikeAnimate {
[self.likeBtn executeAnimation];
}
- (void)updateLikeUI {
[self.likeBtn updateLikeBtnState:self.model.isLike likeCount:self.model.likeCount];
}
- (void)hideNeedlessView {
self.informBtn.hidden = YES;
self.likeBtn.hidden = YES;
self.remarkCountBtn.hidden = YES;
}
#pragma mark - setter
- (void)setModel:(ComDynModel *)model {
_model = model;
[self.userIcon yy_setImageWithURL:[NSURL URLWithString:model.userIcon] placeholder:[UIImage defaultPlaceholderWithSize:CGSizeMake(40, 40)]];
self.userNameLab.text = model.userName;
self.timeLab.text = model.time;
self.contentLab.text = model.content;
[self.likeBtn updateLikeBtnState:model.isLike likeCount:model.likeCount];
[self.remarkCountBtn setTitle:[NSString stringWithFormat:@"%d", model.remarkCount] forState:UIControlStateNormal];
[self.contentLab mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.userIcon.mas_bottom).offset(12);
make.left.equalTo(self.contentView).offset(self.margin);
make.right.equalTo(self.contentView).offset(-self.margin);
make.height.equalTo(@([model contentHeight:self.modelType]));
}];
// 先移除
[self.imgContainerView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj removeFromSuperview];
}];
[self.photos removeAllObjects];
// 再添加
CGFloat imgContainerViewH = [model imgHeight];
[self.imgContainerView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentLab.mas_bottom).offset(12);
make.left.equalTo(self.contentView).offset(self.margin);
make.right.equalTo(self.contentView).offset(-self.margin);
make.height.equalTo(@(imgContainerViewH));
}];
if (imgContainerViewH > 0) {
NSArray *imgUrlArr = [model getImgUrlsArr:model.imgUrls];
CGFloat itemCount = imgUrlArr.count;
CGFloat itemSpace = itemCount > 1 ? (kScreenWidth - 60 - imgContainerViewH * itemCount) / (itemCount - 1) : 0;
for (int index = 0; index < itemCount; index++) {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(index*(imgContainerViewH + itemSpace), 0, imgContainerViewH, imgContainerViewH)];
[imgView cornerRadius:12];
imgView.tag = index;
imgView.userInteractionEnabled = YES;
[imgView yy_setImageWithURL:[NSURL URLWithString:imgUrlArr[index]] placeholder:[UIImage defaultPlaceholderWithSize:imgView.size]];
imgView.dk_alphaPicker = DKAlphaPickerWithAlphas(1.f, 0.5f, 0.1f);
[self.imgContainerView addSubview:imgView];
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lookImg:)];
[imgView addGestureRecognizer:tapGR];
GKPhoto *photo = [GKPhoto new];
photo.url = [NSURL URLWithString:imgUrlArr[index]];
photo.sourceImageView = imgView;
[self.photos addObject:photo];
}
}
}
#pragma mark - private
- (void)lookImg:(UITapGestureRecognizer *)tapGR {
UIImageView *showIV = (UIImageView *)tapGR.view;
GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:[_photos copy] currentIndex:showIV.tag];
browser.showStyle = GKPhotoBrowserShowStyleZoom;
browser.loadStyle = GKPhotoBrowserLoadStyleDeterminate;
[browser showFromVC:self.ds_viewController];
}
#pragma mark - lazy
- (UIImageView *)userIcon {
if (!_userIcon) {
_userIcon = [UIImageView new];
[_userIcon cornerRadius:20];
_userIcon.dk_alphaPicker = DKAlphaPickerWithAlphas(1.f, 0.5f, 0.1f);
}
return _userIcon;
}
- (UILabel *)userNameLab {
if (!_userNameLab) {
_userNameLab = [UILabel labWithFont:BoldFont(15)];
_userNameLab.dk_textColorPicker = DKColorPickerWithColors(MainTextColor, DkTitleColor, DSWhite);
}
return _userNameLab;
}
- (UILabel *)timeLab {
if (!_timeLab) {
_timeLab = [UILabel labWithFont:SysFont(12)];
_timeLab.dk_textColorPicker = DKColorPickerWithColors(SmallTextColor, ColorFromHexA(0xFFFFFF, .3), DSWhite);
}
return _timeLab;
}
- (UIButton *)informBtn {
if (!_informBtn) {
_informBtn = [UIButton btnWithTitle:@"•••" font:BoldFont(15)];
_informBtn.tag = 1;
[_informBtn dk_setTitleColorPicker:DKColorPickerWithColors(MainTextColor, ColorFromHex(0xE8E9E9), DSWhite) forState:UIControlStateNormal];
[_informBtn addTarget:self action:@selector(informOrlikeOrRemarkAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _informBtn;
}
- (UIView *)imgContainerView {
if (!_imgContainerView) {
_imgContainerView = [UIView new];
}
return _imgContainerView;
}
- (UILabel *)contentLab {
if (!_contentLab) {
_contentLab = [UILabel labWithFont:SysFont(14)];
_contentLab.dk_textColorPicker = DKColorPickerWithColors(MainTextColor, ColorFromHexA(0xFFFFFF, .5), DSWhite);
_contentLab.numberOfLines = 0;
}
return _contentLab;
}
- (void)informOrlikeOrRemarkAction:(UIButton *)sender {
if (sender.tag == 1) {
if (self.tapInformBlock) { self.tapInformBlock(); }
} else if (sender.tag == 2) {
if (self.tapLikeBlock) { self.tapLikeBlock(); }
} else {
if (self.tapRemarkBlock) { self.tapRemarkBlock(); }
}
}
- (LikeButton *)likeBtn {
if (!_likeBtn) {
_likeBtn = [[LikeButton alloc] initWithNormalImg:nil nightNormalImg:nil selectedImg:nil nightSelectedImg:nil];
_likeBtn.tag = 2;
[_likeBtn addTarget:self action:@selector(informOrlikeOrRemarkAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _likeBtn;
}
- (UIButton *)remarkCountBtn {
if (!_remarkCountBtn) {
_remarkCountBtn = [UIButton btnWithTitle:@"0" font:SysFont(12)];
_remarkCountBtn.tag = 3;
[_remarkCountBtn dk_setTitleColorPicker:DKColorPickerWithColors(SmallTextColor, ColorFromHexA(0xFFFFFF, .3), DSWhite) forState:UIControlStateNormal];
[_remarkCountBtn dk_setImage:DKImagePickerWithNames(@"ic_shequ_pinlun", @"dk_ic_shequ_pinlun", @"ic_shequ_pinlun") forState:UIControlStateNormal];
_remarkCountBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 2, 0, 0);
[_remarkCountBtn addTarget:self action:@selector(informOrlikeOrRemarkAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _remarkCountBtn;
}
@end