-
Notifications
You must be signed in to change notification settings - Fork 1
/
UIViewCarousel.m
270 lines (209 loc) · 8.07 KB
/
UIViewCarousel.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
//
// UIViewCarousel.m
// Miso
//
// Created by Joshua Wu on 8/29/11.
// Copyright 2011 Miso. All rights reserved.
//
#import "UIViewCarousel.h"
#import "NSDictionary+Convenience.h"
#import <QuartzCore/QuartzCore.h>
@interface UIViewCarousel ()
- (void)bufferViews;
- (BOOL)viewExistAtIndex:(int)index;
- (void)readjustActiveWebviews;
@end
@implementation UIViewCarousel
@synthesize currentIndex=_currentIndex;
@synthesize enableViewBuffer=_enableViewBuffer;
@synthesize showPageControl=_showPageControl;
@synthesize enableWrap=_enableWrap;
@synthesize bufferSize=_bufferSize;
@synthesize dataSource=_dataSource;
@synthesize carousel=_carousel;
@synthesize pageControl=_pageControl;
@synthesize scrollDelegate=_scrollDelegate;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
_bufferSize = 10;
_enableViewBuffer = YES;
_activeViews = [[NSMutableArray array] retain];
_dequeueList = [[NSMutableDictionary dictionary] retain];
_carousel = [[UIScrollView alloc] initWithFrame:self.bounds];
_carousel.delegate = self;
_carousel.pagingEnabled = YES;
_carousel.directionalLockEnabled = YES;
_carousel.alwaysBounceVertical = NO;
_carousel.showsHorizontalScrollIndicator = NO;
_carousel.alwaysBounceHorizontal = YES;
_carousel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:_carousel];
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 20, self.frame.size.width, 20)];
_pageControl.backgroundColor = [UIColor clearColor];
_pageControl.hidden = YES;
[self addSubview:_pageControl];
self.backgroundColor = [UIColor clearColor];
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.currentIndex = 0;
}
return self;
}
- (void)dealloc {
[_activeViews release];
[_carousel release];
[_dequeueList release];
[super dealloc];
}
#pragma mark - Properties
- (void)setCurrentIndex:(int)currentIndex animated:(BOOL)animated {
[AnalyticsController logEvent:kAnalyticsViewSSItem];
_currentIndex = currentIndex;
_pageControl.currentPage = _currentIndex;
[self bufferViews];
if (animated) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
}
_carousel.contentOffset = CGPointMake(_carousel.frame.size.width * _currentIndex, 0);
if (animated) {
[UIView commitAnimations];
}
}
- (void)setCurrentIndex:(int)currentIndex {
[self setCurrentIndex:currentIndex animated:YES];
}
- (void)setShowPageControl:(BOOL)showPageControl {
_showPageControl = showPageControl;
if (_showPageControl) {
_pageControl.hidden = NO;
} else {
_carousel.frame = self.bounds;
_pageControl.hidden = YES;
}
}
- (void)setEnableViewBuffer:(BOOL)enableViewBuffer {
_enableViewBuffer = enableViewBuffer;
if (_enableViewBuffer)
_bufferSize = 10;
else
_bufferSize = [_dataSource numberOfViewsInViewCarousel:self];
}
#pragma mark - Public Methods
- (UIView *)activeViewWithTag:(NSInteger)tag {
for (UIView *view in _activeViews) {
if (view.tag == tag) {
return view;
}
}
return nil;
}
- (void)reloadData {
for (UIView *view in _activeViews) {
NSString *classKey = NSStringFromClass([view class]);
NSMutableArray *dequeueArray = [_dequeueList objectOrNilForKey:classKey];
if(dequeueArray == nil) {
dequeueArray = [NSMutableArray array];
[_dequeueList setObject:dequeueArray forKey:classKey];
}
[view removeFromSuperview];
[dequeueArray addObject:view];
}
[_activeViews removeAllObjects];
_numViews = [_dataSource numberOfViewsInViewCarousel:self];
_pageControl.numberOfPages = _numViews;
int rootIndex = 0;
if (_enableWrap) { _numViews += 2; rootIndex = 1;}
_carousel.contentSize = CGSizeMake(_carousel.frame.size.width * _numViews, _carousel.frame.size.height);
[self setCurrentIndex:rootIndex animated:NO];
[self bufferViews];
}
- (id)dequeueReuseableViewWithClass:(id)klass {
NSString *classKey = NSStringFromClass([klass class]);
NSMutableArray *dequeueArray = [_dequeueList objectOrNilForKey:classKey];
if (dequeueArray && [dequeueArray count] > 0) {
id view = [[[dequeueArray objectAtIndex:0] retain] autorelease];
[dequeueArray removeObjectAtIndex:0];
return view;
} else
return nil;
}
#pragma mark - Private Methods
- (void)readjustActiveWebviews {
NSMutableArray *viewsToRemove = [NSMutableArray array];
for (UIView *view in _activeViews) {
if (abs(_currentIndex - view.tag) > _bufferSize) {
[viewsToRemove addObject:view];
[view removeFromSuperview];
NSString *classKey = NSStringFromClass([view class]);
NSMutableArray *dequeueArray = [_dequeueList objectOrNilForKey:classKey];
if(dequeueArray == nil) {
dequeueArray = [NSMutableArray array];
[_dequeueList setObject:dequeueArray forKey:classKey];
}
[dequeueArray addObject:view];
}
}
[_activeViews removeObjectsInArray:viewsToRemove];
}
- (BOOL)viewExistAtIndex:(int)index {
for (UIView *view in _activeViews) {
if (view.tag == index) {
return YES;
}
}
return NO;
}
- (void)bufferViews {
[self readjustActiveWebviews];
if ([_dataSource numberOfViewsInViewCarousel:self] == 0) { return; }
for (int i = _currentIndex - _bufferSize; i <= _currentIndex + _bufferSize; i++) {
if (i < 0 || i >= _numViews || [self viewExistAtIndex:i])
continue;
else {
int askIndex = i;
if (_enableWrap) {
int tempNumViews = [_dataSource numberOfViewsInViewCarousel:self];
askIndex = (((i-1)%tempNumViews)+tempNumViews)%tempNumViews;
}
UIView *view = [_dataSource viewCarousel:self viewAtIndex:askIndex];
if (view != nil) {
view.tag = i;
view.frame = CGRectMake(_carousel.frame.size.width * i, 0, view.frame.size.width, view.frame.size.height);
[view setNeedsDisplay];
[_activeViews addObject:view];
[_carousel addSubview:view];
}
}
}
}
#pragma mark - UIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
int index = round(_carousel.contentOffset.x / _carousel.frame.size.width);
if (index != _currentIndex) {
_currentIndex = index;
if (_enableWrap) {
if (_currentIndex == 0) {
_carousel.contentOffset = CGPointMake(_carousel.contentOffset.x + (_numViews - 2) * _carousel.frame.size.width, _carousel.contentOffset.y);
} else if (_currentIndex == _numViews - 1) {
_carousel.contentOffset = CGPointMake(_carousel.contentOffset.x - (_numViews - 2) * _carousel.frame.size.width, _carousel.contentOffset.y);
}
}
[self bufferViews];
}
if ([_scrollDelegate respondsToSelector:@selector(scrollViewDidScroll:)]) {
[_scrollDelegate scrollViewDidScroll:scrollView];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
int index = round(_carousel.contentOffset.x / _carousel.frame.size.width);
if (_enableWrap) {
int tempNumViews = [_dataSource numberOfViewsInViewCarousel:self];
_pageControl.currentPage = (((index-1)%tempNumViews)+tempNumViews)%tempNumViews;
} else {
_pageControl.currentPage = index;
}
if ([_scrollDelegate respondsToSelector:@selector(scrollViewDidEndDecelerating:)]) {
[_scrollDelegate scrollViewDidEndDecelerating:scrollView];
}
}
@end