forked from appsome/AccordionView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccordionView.m
executable file
·255 lines (202 loc) · 8.86 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
/*
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, views, headers, originalSizes, scrollView;
@synthesize allowsMultipleSelection, selectionIndexes, delegate, allowsUITableSectionStyle,openFirstIndex;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
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.allowsUITableSectionStyle = NO;
self.openFirstIndex = true;
}
return self;
}
- (void)addHeader:(id)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];
}
// to have flexibility of not opening 1st tab as default. by default is TRUE
if ([selectionIndexes count] == 0 && self.openFirstIndex && selectedIndex == 0) {
[self setSelectedIndex:0];
}
}
}
- (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)touchDown:(id)sender {
if (allowsMultipleSelection) {
// init the indexSet. otherwise, 1st time "addIndex" will not work
if(selectionIndexes == nil){
selectionIndexes = [[NSMutableIndexSet alloc] init];
}
NSMutableIndexSet *mis = [selectionIndexes mutableCopy];
if ([selectionIndexes containsIndex:[sender tag]]) {
[mis removeIndex:[sender tag]];
} else {
[mis addIndex:[sender tag]];
}
[self setSelectionIndexes:mis];
} 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]) {
viewFrame.size.height = originalSize.height;
[aView setFrame:CGRectMake(0, viewFrame.origin.y, [self frame].size.width, 0)];
[aView setHidden:NO];
} 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;
}
}
[scrollView setContentOffset:offset animated:YES];
[self scrollViewDidScroll:scrollView];
}
}
#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 && allowsUITableSectionStyle) {
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