-
Notifications
You must be signed in to change notification settings - Fork 38
/
AccordionView.m
326 lines (259 loc) · 10.7 KB
/
AccordionView.m
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
/*
AccordionView.m
Created by Wojtek Siudzinski on 19.12.2011.
Copyright (c) 2011 Appsome. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "AccordionView.h"
@implementation AccordionView
@synthesize selectedIndex, isHorizontal, animationDuration, animationCurve;
@synthesize allowsMultipleSelection, selectionIndexes, delegate, startsClosed, allowsEmptySelection;
@synthesize scrollView;
-(void)initAccordion{
views = [NSMutableArray new];
headers = [NSMutableArray new];
originalSizes = [NSMutableArray new];
self.backgroundColor = [UIColor clearColor];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [self frame].size.width, [self frame].size.height)];
scrollView.backgroundColor = [UIColor clearColor];
[self addSubview:scrollView];
self.userInteractionEnabled = YES;
scrollView.userInteractionEnabled = YES;
animationDuration = 0.3;
animationCurve = UIViewAnimationCurveEaseIn;
self.autoresizesSubviews = NO;
scrollView.autoresizesSubviews = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
self.allowsMultipleSelection = NO;
self.startsClosed = NO;
self.allowsEmptySelection = YES;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initAccordion];
}
return self;
}
/***
NIB constructor
**/
-(id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
[self initAccordion];
}
return self;
}
- (void)addHeader:(UIControl *)aHeader withView:(id)aView {
if ((aHeader != nil) && (aView != nil)) {
[headers addObject:aHeader];
[views addObject:aView];
[originalSizes addObject:[NSValue valueWithCGSize:[aView frame].size]];
[aView setAutoresizingMask:UIViewAutoresizingNone];
[aView setClipsToBounds:YES];
CGRect frame = [aHeader frame];
if (self.isHorizontal) {
// TODO
} else {
frame.origin.x = 0;
frame.size.width = [self frame].size.width;
[aHeader setFrame:frame];
frame = [aView frame];
frame.origin.x = 0;
frame.size.width = [self frame].size.width;
[aView setFrame:frame];
}
[scrollView addSubview:aView];
[scrollView addSubview:aHeader];
if ([aHeader respondsToSelector:@selector(addTarget:action:forControlEvents:)]) {
[aHeader setTag:[headers count] - 1];
[aHeader addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchUpInside];
}
if (!self.startsClosed && [selectionIndexes count] == 0) {
[self setSelectedIndex:0];
}
}
}
- (void)removeHeaderAtIndex:(NSInteger)index {
if (index > [headers count] - 1) return;
NSMutableIndexSet *cleanIndexes = [NSMutableIndexSet new];
[selectionIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
if (idx == index) return;
if (idx > index) idx--;
[cleanIndexes addIndex:idx];
}];
for (UIView *aHeader in headers) {
if (aHeader.tag > index) {
aHeader.tag--;
}
}
UIView *aHeader = [headers objectAtIndex:index];
[aHeader removeFromSuperview];
[headers removeObjectAtIndex:index];
UIView *aView = [views objectAtIndex:index];
[aView removeFromSuperview];
[views removeObjectAtIndex:index];
[originalSizes removeObjectAtIndex:index];
[self setSelectionIndexes:cleanIndexes];
}
- (void)setSelectionIndexes:(NSIndexSet *)aSelectionIndexes {
if ([headers count] == 0) return;
if (!allowsMultipleSelection && [aSelectionIndexes count] > 1) {
aSelectionIndexes = [NSIndexSet indexSetWithIndex:[aSelectionIndexes firstIndex]];
}
NSMutableIndexSet *cleanIndexes = [NSMutableIndexSet new];
[aSelectionIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
if (idx > [headers count] - 1) return;
[cleanIndexes addIndex:idx];
}];
selectionIndexes = cleanIndexes;
[self setNeedsLayout];
if ([delegate respondsToSelector:@selector(accordion:didChangeSelection:)]) {
[delegate accordion:self didChangeSelection:self.selectionIndexes];
}
}
- (void)setSelectedIndex:(NSInteger)aSelectedIndex {
[self setSelectionIndexes:[NSIndexSet indexSetWithIndex:aSelectedIndex]];
}
- (NSInteger)selectedIndex {
return [selectionIndexes firstIndex];
}
- (void)setOriginalSize:(CGSize)size forIndex:(NSUInteger)index {
if (index >= [views count]) return;
[originalSizes replaceObjectAtIndex:index withObject:[NSValue valueWithCGSize:size]];
if ([selectionIndexes containsIndex:index]) [self setNeedsLayout];
}
- (void)setStartsClosed:(BOOL)itStartsClosed {
if (itStartsClosed) {
[self setSelectionIndexes:[NSIndexSet indexSet]];
}
startsClosed = itStartsClosed;
}
- (void)touchDown:(id)sender {
if (allowsMultipleSelection) {
NSMutableIndexSet *mis = [selectionIndexes mutableCopy];
if ([selectionIndexes containsIndex:[sender tag]]) {
if (self.allowsEmptySelection || [selectionIndexes count] > 1) {
[mis removeIndex:[sender tag]];
}
} else {
[mis addIndex:[sender tag]];
}
[self setSelectionIndexes:mis];
} else {
// If the touched section is already opened, close it.
if (([selectionIndexes firstIndex] == [sender tag]) && self.allowsEmptySelection) {
[self setSelectionIndexes:[NSIndexSet indexSet]];
} else {
[self setSelectedIndex:[sender tag]];
}
}
}
- (void)animationDone {
for (int i=0; i<[views count]; i++) {
if (![selectionIndexes containsIndex:i]) [[views objectAtIndex:i] setHidden:YES];
}
}
- (void)layoutSubviews {
if (self.isHorizontal) {
// TODO
} else {
int height = 0;
for (int i=0; i<[views count]; i++) {
id aHeader = [headers objectAtIndex:i];
id aView = [views objectAtIndex:i];
CGSize originalSize = [[originalSizes objectAtIndex:i] CGSizeValue];
CGRect viewFrame = [aView frame];
CGRect headerFrame = [aHeader frame];
headerFrame.origin.y = height;
height += headerFrame.size.height;
viewFrame.origin.y = height;
if ([selectionIndexes containsIndex:i]) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:self.animationDuration];
[UIView setAnimationCurve:self.animationCurve];
[UIView setAnimationBeginsFromCurrentState:YES];
viewFrame.size.height = originalSize.height;
[aView setFrame:CGRectMake(0, viewFrame.origin.y, [self frame].size.width, 0)];
[aView setHidden:NO];
[UIView commitAnimations];
} else {
viewFrame.size.height = 0;
}
height += viewFrame.size.height;
if (!CGRectEqualToRect([aHeader frame], headerFrame) || !CGRectEqualToRect([aView frame], viewFrame)) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDone)];
[UIView setAnimationDuration:self.animationDuration];
[UIView setAnimationCurve:self.animationCurve];
[UIView setAnimationBeginsFromCurrentState:YES];
[aHeader setFrame:headerFrame];
[aView setFrame:viewFrame];
[UIView commitAnimations];
}
}
CGPoint offset = scrollView.contentOffset;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:self.animationDuration];
[UIView setAnimationCurve:self.animationCurve];
[UIView setAnimationBeginsFromCurrentState:YES];
[scrollView setContentSize:CGSizeMake([self frame].size.width, height)];
[UIView commitAnimations];
if (offset.y + scrollView.frame.size.height > height) {
offset.y = height - scrollView.frame.size.height;
if (offset.y < 0) {
offset.y = 0;
}
}
if ([selectionIndexes firstIndex] && [selectionIndexes firstIndex] < headers.count && self.autoScrollToTopOnSelect)
{
offset = ((UIButton *)headers[[selectionIndexes firstIndex]]).frame.origin;
}
[scrollView setContentOffset:offset animated:YES];
[self scrollViewDidScroll:scrollView];
}
}
- (void)setFrame:(CGRect)frame{
[super setFrame:frame];
scrollView.frame = frame;
}
#pragma mark UIScrollView delegate
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
int i = 0;
for (UIView *view in views) {
if (self.isHorizontal) {
// TODO
} else {
if (view.frame.size.height > 0) {
UIView *header = [headers objectAtIndex:i];
CGRect content = view.frame;
content.origin.y -= header.frame.size.height;
content.size.height += header.frame.size.height;
CGRect frame = header.frame;
if (CGRectContainsPoint(content, aScrollView.contentOffset)) {
if (aScrollView.contentOffset.y < content.origin.y + content.size.height - frame.size.height) {
frame.origin.y = aScrollView.contentOffset.y;
} else {
frame.origin.y = content.origin.y + content.size.height - frame.size.height;
}
} else {
frame.origin.y = view.frame.origin.y - frame.size.height;
}
header.frame = frame;
}
}
i++;
}
}
@end