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
//
// ICRAttachmentView.m
// XFFruit
//
// Created by Xummer on 15/4/15.
// Copyright (c) 2015年 Xummer. All rights reserved.
//
#import "ICRAttachmentView.h"
#import "ICRAttachTitleView.h"
#define ADD_BTN_WIDTH (44.0f)
#define ATTVIEW_INNER_GAP (5.0f)
@interface ICRAttachmentView ()
{
ICRAttViewType m_eViewType;
}
@property (strong, nonatomic) ICRAttachTitleView *m_titleView;
@end
@implementation ICRAttachmentView
#pragma mark - Life Cycle
- (instancetype)initWithType:(ICRAttViewType)eType {
self = [super initWithFrame:CGRectZero];
if (!self) {
return nil;
}
m_eViewType = eType;
self.m_arrAttViews = [NSMutableArray array];
[self initSubviews];
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (!self) {
return nil;
}
self.m_arrAttViews = [NSMutableArray array];
[self initSubviews];
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat fDelta = 10;
_m_titleView.frame = (CGRect){
.origin.x = 0,
.origin.y = (self.height - ICRATTACH_TITLE_VIEW_HEIGHT) * .5f + fDelta,
.size.width = ICRATTACH_TITLE_VIEW_WIDTH,
.size.height = ICRATTACH_TITLE_VIEW_HEIGHT
};
_m_addButton.frame = (CGRect){
.origin.x = self.width - ADD_BTN_WIDTH,
.origin.y = (self.height - ADD_BTN_WIDTH) * .5f + fDelta,
.size.width = ADD_BTN_WIDTH,
.size.height = ADD_BTN_WIDTH
};
CGFloat fDx = _m_titleView.right + ATTVIEW_INNER_GAP;
_m_scrollView.frame = (CGRect){
.origin.x = fDx,
.origin.y = 0,
.size.width = _m_addButton.left - fDx - ATTVIEW_INNER_GAP,
.size.height = self.height
};
}
#pragma mark - Public Method
- (void)addContentAttachmentView:(ICRAttachmentUnit *)attView {
UIView *lastView = [_m_arrAttViews lastObject];
CGFloat fX = (lastView ? lastView.right : 0) + ATTVIEW_INNER_GAP;
attView.origin = (CGPoint){
.x = fX,
.y = (_m_scrollView.height - attView.height) * .5f
};
attView.m_uiAttachIndex = [_m_arrAttViews count];
attView.m_closeButton.tag = attView.m_uiAttachIndex;
[attView.m_closeButton addTarget:self action:@selector(onCloseButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
[self.m_scrollView addSubview:attView];
[self.m_arrAttViews addObject:attView];
_m_scrollView.contentSize = CGSizeMake(attView.right + ATTVIEW_INNER_GAP, _m_scrollView.height);
[self checkIsReachedMaxCount];
}
- (void)removeContentAttachmentViewAtIndex:(NSUInteger)uiIndex {
if (uiIndex < [_m_arrAttViews count]) {
ICRAttachmentUnit *unit = _m_arrAttViews[ uiIndex ];
[self removeContentAttachmentView:unit];
}
}
- (void)removeContentAttachmentView:(ICRAttachmentUnit *)attView {
[attView removeFromSuperview];
[_m_arrAttViews removeObject:attView];
[self checkIsReachedMaxCount];
[self relayoutContentView];
}
- (void)checkIsReachedMaxCount {
if (_m_uiMaxAttCount > 0) {
self.m_addButton.hidden = [_m_arrAttViews count] >= _m_uiMaxAttCount;
}
}
- (void)relayoutContentView {
NSUInteger uiIndex = 0;
CGFloat fX = ATTVIEW_INNER_GAP;
for (ICRAttachmentUnit *attV in _m_arrAttViews) {
attV.m_uiAttachIndex = uiIndex;
attV.m_closeButton.tag = uiIndex;
attV.origin = (CGPoint){
.x = fX,
.y = (_m_scrollView.height - attV.height) * .5f
};
fX = attV.right + ATTVIEW_INNER_GAP;
}
_m_scrollView.contentSize = CGSizeMake(fX, _m_scrollView.height);
}
#pragma mark - Private Method
- (void)initSubviews {
self.m_titleView = [[ICRAttachTitleView alloc] init];
[self addSubview:_m_titleView];
self.m_scrollView = [[IBTUIScrollView alloc] init];
[self addSubview:_m_scrollView];
self.m_addButton = [IBTUIButton buttonWithType:UIButtonTypeCustom];
[self addSubview:_m_addButton];
self.m_arrAttViews = [NSMutableArray array];
NSString *nsTitle = nil;
NSString *nsIconName = nil;
NSString *nsAddBtnName = nil;
switch (m_eViewType) {
case kAttViewImage:
{
nsTitle = [IBTCommon localizableString:@"Add Photo"];
nsIconName = @"AttachCamera";
nsAddBtnName = @"AttachmentAddBtn";
}
break;
case kAttViewTask:
{
nsTitle = [IBTCommon localizableString:@"Related Task"];
nsIconName = @"AttachCamera";
nsAddBtnName = @"AttachmentAddBtn";
}
break;
case kAttViewVoice:
{
nsTitle = [IBTCommon localizableString:@"Record"];
nsIconName = @"RecordIcon";
nsAddBtnName = @"AttachmentAddBtn";
}
break;
default:
break;
}
[_m_addButton setImage:[UIImage imageNamed:nsAddBtnName]
forState:UIControlStateNormal];
[_m_titleView updateWithTitle:nsTitle iconName:nsIconName];
}
#pragma mark - Action
- (void)onCloseButtonAction:(id)sender {
UIButton *closeBtn = sender;
[self removeContentAttachmentViewAtIndex:closeBtn.tag];
}
@end