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
//
// PreListView.m
// Gengmei
//
// Created by Sean Lee on 4/7/15.
// Copyright (c) 2015 Wanmeichuangyi. All rights reserved.
//
#import "GMPreviewListView.h"
#import <Masonry/Masonry.h>
#import <Constant.h>
@interface GMPreviewListView ()<GMPreviewCellDelegate, UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) GMPreviewCell *defaultCell;
@property (nonatomic, assign) BOOL hasDelete;
@property (nonatomic, assign) BOOL frontMove;
@property (nonatomic, assign) int moveCount;
@end
@implementation GMPreviewListView
- (void)setup{
[super setup];
self.backgroundColor = UIColor.whiteColor;
_previewList = [[NSMutableArray alloc] init];
_hasDelete = NO;
_moveCount = 0;
_scrollView = [[UIScrollView alloc] init];
_scrollView.delegate = self;
_scrollView.backgroundColor = UIColor.whiteColor;
_scrollView.scrollEnabled = YES;
_scrollView.directionalLockEnabled = YES;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.showsHorizontalScrollIndicator = YES;
// _scrollView.alwaysBounceHorizontal = YES;
_scrollView.contentSize = [self contentSizeForUIScrollView:0];
[self addSubview:_scrollView];
if (!_cellWH) {
_cellWH = self.cellWH;
}
_defaultCell = [[GMPreviewCell alloc] initWithFrame:CGRectMake(0, 0,PREVIRE_CELL_HEIGHT, PREVIRE_CELL_HEIGHT) image:[UIImage imageNamed:@"camera"]];
_defaultCell.tag = 1000;
_defaultCell.delegate = self;
[_scrollView addSubview:_defaultCell];
[self scrollViewAbleScroll];
}
- (void)setDefaultCellImage:(UIImage *)defaultCellImage {
_defaultCellImage = defaultCellImage;
if (_defaultCellImage != nil) {
_defaultCell.image = _defaultCellImage;
}
}
- (NSInteger)cellWH {
if (_cellWH > 0) {
CGRect rect = _defaultCell.frame;
rect.size.width = _cellWH;
rect.size.height = _cellWH;
_defaultCell.frame = rect;
} else {
_cellWH = PREVIRE_CELL_HEIGHT;
}
return _cellWH;
}
- (void)updateConstraints{
[_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(00);
make.top.mas_equalTo(00);
make.right.mas_equalTo(0);
make.bottom.mas_equalTo(0);
}];
[super updateConstraints];
}
/*
* @brief 根据index获取UIScrollView的ContentSize
*/
- (CGSize)contentSizeForUIScrollView:(NSInteger)index
{
float width = (PREVIRE_CELL_SPACE + self.cellWH) * index;
if (width < _scrollView.bounds.size.width)
width = _scrollView.bounds.size.width;
return CGSizeMake(width, PREVIRE_CELL_HEIGHT);
}
- (void)scrollViewAbleScroll
{
_scrollView.contentSize = [self contentSizeForUIScrollView:(_previewList.count + 1)];
[_scrollView scrollRectToVisible:CGRectMake(_scrollView.contentSize.width - self.cellWH, 0, self.cellWH, self.frame.size.height) animated:YES];
}
- (GMPreviewCell * ) addImage:(UIImage *)image{
GMPreviewCell * newPreviewCell = nil;
CGFloat x = (_previewList.count) * (PREVIRE_CELL_SPACE + self.cellWH);
newPreviewCell = [[GMPreviewCell alloc] initWithFrame:CGRectMake(x, 0, self.cellWH, self.cellWH) image:image];
newPreviewCell.alpha = 0.1;
newPreviewCell.delegate = self;
[_previewList addObject:newPreviewCell];
[_scrollView addSubview:newPreviewCell];
[self scrollViewAbleScroll];
_defaultCell.alpha = 0.5;
[UIView animateWithDuration:DURATION animations:^(){
CGRect rect = _defaultCell.frame;
rect.origin.x += (PREVIRE_CELL_SPACE + self.cellWH);
_defaultCell.frame = rect;
_defaultCell.alpha = 1.0;
newPreviewCell.alpha = 0.8;
} completion:^(BOOL finished){
newPreviewCell.alpha = 1.0;
}];
return newPreviewCell;
}
#pragma -- mark PreviewCellDelegate
- (void)prepareAddPreviewCell:(GMPreviewCell *)cell{
if ([self.delegate respondsToSelector:@selector(addPreviewCell)]) {
[self.delegate addPreviewCell];
}
}
- (void)prepareDeletePreviewCell:(GMPreviewCell *)cell{
NSInteger index = [_previewList indexOfObject:cell];
//设置相关Cell的透明度
cell.alpha = 0.8;
// 判断其余cell的移动方向(从前向后移动/从后向前移动)
_frontMove = NO;
if (_previewList.count - 1 > DEFAULT_VISIBLE_COUNT
&& (_previewList.count - index - 1) <= DEFAULT_VISIBLE_COUNT) {
_frontMove = YES;
}
if (index == _previewList.count - 1 && !_frontMove)
_defaultCell.alpha = 0.5;
[UIView animateWithDuration:DURATION animations:^(){
//其余defautlCell依次移动
if (_frontMove)
{
//前面的向后移动
for (int i = 0; i < index; i++) {
GMPreviewCell *cell = _previewList[i];
CGRect rect = cell.frame;
rect.origin.x += (PREVIRE_CELL_SPACE + self.cellWH);
cell.frame = rect;
}
_moveCount++;
}
else
{
//后面的向前移动
for (NSInteger i = index + 1; i < _previewList.count; i++) {
GMPreviewCell *cell = _previewList[i];
CGRect rect = cell.frame;
rect.origin.x -= (PREVIRE_CELL_SPACE + self.cellWH);
cell.frame = rect;
}
//defautlCell向前移动
CGRect rect = _defaultCell.frame;
rect.origin.x -= (PREVIRE_CELL_SPACE + self.cellWH);
_defaultCell.frame = rect;
_defaultCell.alpha = 1.0;
}
cell.alpha = 0.0;
} completion:^(BOOL finished){
if ([self.delegate respondsToSelector:@selector(deletePreviewCell:)]) {
[self.delegate deletePreviewCell:index];
}
//删除被点击的cell
[cell removeFromSuperview];
[_previewList removeObject:cell];
if (_previewList.count <= DEFAULT_VISIBLE_COUNT){
[_scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
_scrollView.contentSize = [self contentSizeForUIScrollView:0];
}
if (_frontMove) {
[self isNeedResetFrame];
}
}];
_hasDelete = YES;
}
/**大图模式*/
- (void)didSelectedPreviewCell:(GMPreviewCell *)cell{
if ([self.delegate respondsToSelector:@selector(fullScreenImage:)]) {
[self.delegate fullScreenImage:cell];
}
}
- (void)prepareRetryUploadPreviewCell:(GMPreviewCell *)cell{
if ([self.delegate respondsToSelector:@selector(retryUploadPreviewCell:)]) {
[self.delegate retryUploadPreviewCell:cell];
}
}
/**
* @brief 当删除操作是前面的unitCell向后移动时
* 做滚动操作或者添加操作需要重新设置每个unitCell的frame
*/
- (void)isNeedResetFrame
{
if (_frontMove && _moveCount > 0) {
for (int i = 0; i < _previewList.count; i++) {
GMPreviewCell *cell = [_previewList objectAtIndex:(NSUInteger) i];
CGRect rect = cell.frame;
rect.origin.x -= (PREVIRE_CELL_SPACE + self.cellWH) * _moveCount;
cell.frame = rect;
}
CGRect rect = _defaultCell.frame;
rect.origin.x -= (PREVIRE_CELL_SPACE + self.cellWH) * _moveCount;
_defaultCell.frame = rect;
_frontMove = NO;
_moveCount = 0;
}
if (_hasDelete)
{
_scrollView.contentSize = [self contentSizeForUIScrollView:(_previewList.count + 1)];
_hasDelete = !_hasDelete;
}
}
/*** @brief 开始上传,显示loading */
- (void)showLoadingAtIndex:(NSInteger)index{
GMPreviewCell * cell = _previewList[index];
[cell.retryButton setHidden:YES];
[cell.deleteButton setHidden:YES];
[cell.indicatorView setHidden:NO];
[cell.indicatorView startAnimating];
}
/*** @brief 上传成功,隐藏loading */
- (void)hideLoadingAtIndex:(NSInteger)index{
GMPreviewCell * cell = _previewList[index];
[cell.retryButton setHidden:YES];
[cell.deleteButton setHidden:NO];
[cell.indicatorView setHidden:YES];
[cell.indicatorView stopAnimating];
}
/*** @brief 重新上传loading */
- (void)retryLoadingAtIndex:(NSInteger)index{
if (index > _previewList.count - 1){
return;
}
GMPreviewCell * cell = _previewList[index];
[cell.retryButton setHidden:NO];
[cell.deleteButton setHidden:NO];
[cell.indicatorView setHidden:YES];
[cell.indicatorView stopAnimating];
}
@end