Skip to content

Commit d7425f8

Browse files
committed
first commit
0 parents  commit d7425f8

File tree

4 files changed

+360
-0
lines changed

4 files changed

+360
-0
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
ise
3+
*.mode1v3
4+
*.pbxuser
5+
*.perspective
6+
*.perspectivev3
7+
*.pyc
8+
*~.nib/
9+
build/*
10+
11+
# old school
12+
.svn
13+
14+
# osx noise
15+
.DS_Store
16+
profile
17+

README

Whitespace-only changes.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// VCThumbnailGridView.h
3+
//
4+
//
5+
// Created by Vinay Chavan on 19/03/11.
6+
// Copyright 2011 Vinay Chavan. All rights reserved.
7+
//
8+
9+
/*
10+
* create UIThumbnailGridViewItem { image or imageUrl }
11+
* if items go out of the screen, remove subviews representing those items
12+
* add scroll delegate methods to check which cells are turning visible or going out of the screen
13+
*
14+
*/
15+
16+
#import <UIKit/UIKit.h>
17+
18+
enum VCThumbnailGridViewStyle {
19+
VCThumbnailGridViewStyleGrid = 0,
20+
VCThumbnailGridViewStyleStripe = 1
21+
};
22+
typedef NSUInteger VCThumbnailGridViewStyle;
23+
24+
@protocol VCThumbnailGridViewDataSource;
25+
@protocol VCThumbnailGridViewDelegate;
26+
27+
@class URLImageView;
28+
29+
@interface VCThumbnailGridView : UIScrollView {
30+
id<VCThumbnailGridViewDelegate> _gridDelegate;
31+
id<VCThumbnailGridViewDataSource> _gridDataSource;
32+
VCThumbnailGridViewStyle _style;
33+
34+
NSInteger _numberOfThumbnails;
35+
NSInteger _numberOfThumbnailsInRow;
36+
CGFloat _thumbnailHeight;
37+
CGFloat _thumbnailWidth;
38+
CGFloat _thumbnailBorderWidth;
39+
40+
NSMutableArray *_thumbnails;
41+
UIView *_footerView;
42+
}
43+
44+
@property (nonatomic, assign) id<VCThumbnailGridViewDelegate> gridDelegate;
45+
@property (nonatomic, assign) id<VCThumbnailGridViewDataSource> gridDataSource;
46+
@property (nonatomic, assign) VCThumbnailGridViewStyle style;
47+
@property (nonatomic, assign) CGFloat rowHeight;
48+
@property (nonatomic, retain) UIView *footerView;
49+
50+
- (void)reloadData;
51+
52+
@end
53+
54+
55+
@protocol VCThumbnailGridViewDataSource <NSObject>
56+
57+
@required
58+
- (NSInteger)numberOfThumbnailsInThumbnailGridView:(VCThumbnailGridView*)thumbnailGridView;
59+
60+
@optional
61+
- (NSInteger)numberOfThumbnailsInRowInThumbnailGridView:(VCThumbnailGridView*)thumbnailGridView;
62+
- (UIImage*)thumbnailGridView:(VCThumbnailGridView*)thumbnailGridView imageAtIndex:(NSInteger)index;
63+
- (NSString*)thumbnailGridView:(VCThumbnailGridView*)thumbnailGridView imageUrlAtIndex:(NSInteger)index;
64+
- (NSString*)thumbnailGridView:(VCThumbnailGridView *)thumbnailGridView titleForThumbnailAtIndex:(NSInteger)index;
65+
66+
@end
67+
68+
@protocol VCThumbnailGridViewDelegate <NSObject>
69+
70+
@optional
71+
- (void)thumbnailGridView:(VCThumbnailGridView*)thumbnailGridView didSelectThumbnailAtIndex:(NSInteger)index;
72+
73+
@end
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
//
2+
// VCThumbnailGridView.m
3+
//
4+
//
5+
// Created by Vinay Chavan on 19/03/11.
6+
// Copyright 2011 Vinay Chavan. All rights reserved.
7+
//
8+
9+
#import "VCThumbnailGridView.h"
10+
11+
#import <QuartzCore/QuartzCore.h>
12+
13+
#import "URLImageView.h"
14+
15+
@interface VCThumbnailGridView()
16+
17+
- (void)createGrid;
18+
- (void)createStripe;
19+
20+
@end
21+
22+
@implementation VCThumbnailGridView
23+
24+
@synthesize gridDelegate = _gridDelegate, gridDataSource = _gridDataSource, style = _style, rowHeight = _thumbnailHeight, footerView = _footerView;
25+
26+
- (id)initWithFrame:(CGRect)frame {
27+
self = [super initWithFrame:frame];
28+
if (self) {
29+
// Initialization code
30+
_gridDelegate = nil;
31+
_gridDataSource = nil;
32+
_footerView = nil;
33+
34+
_numberOfThumbnailsInRow = 4;
35+
_style = VCThumbnailGridViewStyleGrid;
36+
37+
_thumbnails = nil;
38+
}
39+
return self;
40+
}
41+
42+
- (void)layoutSubviews {
43+
if (_thumbnails == nil) {
44+
// need to read data from the delegates
45+
if (_style == VCThumbnailGridViewStyleGrid) {
46+
[self createGrid];
47+
}else if (_style == VCThumbnailGridViewStyleStripe) {
48+
[self createStripe];
49+
}
50+
}
51+
}
52+
53+
54+
- (void)dealloc {
55+
[_thumbnails release], _thumbnails = nil;
56+
[_footerView release], _footerView = nil;
57+
[super dealloc];
58+
}
59+
60+
61+
62+
#pragma mark - Public Methods
63+
64+
- (void)reloadData {
65+
[_thumbnails removeAllObjects];
66+
[_thumbnails release], _thumbnails = nil;
67+
68+
// remove all subviews
69+
for (UIView *subView in self.subviews) {
70+
[subView performSelector:@selector(removeFromSuperview)];
71+
}
72+
73+
[self setNeedsLayout];
74+
}
75+
76+
- (void)setFooterView:(UIView *)footerView {
77+
[_footerView removeFromSuperview];
78+
79+
[_footerView release], _footerView = nil;
80+
_footerView = [footerView retain];
81+
82+
// add subview
83+
[self setNeedsLayout];
84+
}
85+
86+
87+
#pragma mark - Private Methods
88+
89+
- (void)createGrid {
90+
// read number of images
91+
if ([self.gridDataSource respondsToSelector:@selector(numberOfThumbnailsInThumbnailGridView:)]) {
92+
_numberOfThumbnails = [self.gridDataSource numberOfThumbnailsInThumbnailGridView:self];
93+
94+
_numberOfThumbnails = _numberOfThumbnails < 0 ? 0 : _numberOfThumbnails;
95+
}
96+
97+
if ([self.gridDataSource respondsToSelector:@selector(numberOfThumbnailsInRowInThumbnailGridView:)]) {
98+
_numberOfThumbnailsInRow = [self.gridDataSource numberOfThumbnailsInRowInThumbnailGridView:self];
99+
100+
_numberOfThumbnailsInRow = _numberOfThumbnailsInRow <= 0 ? 4 : _numberOfThumbnailsInRow;
101+
}
102+
103+
// calculate sizes for image and border
104+
_thumbnailBorderWidth = 0;
105+
106+
_thumbnailWidth = self.bounds.size.width / (CGFloat)_numberOfThumbnailsInRow;
107+
_thumbnailHeight = _thumbnailWidth + 10;
108+
109+
// resize the scrollable area
110+
NSInteger _numberOfRows = roundf((CGFloat)_numberOfThumbnails / (CGFloat)_numberOfThumbnailsInRow);
111+
self.contentSize = CGSizeMake(self.bounds.size.width,
112+
_thumbnailHeight * _numberOfRows + _footerView.bounds.size.height);
113+
if (_footerView) {
114+
CGRect footerFrame = _footerView.bounds;
115+
footerFrame.origin.y = self.contentSize.height - _footerView.bounds.size.height;
116+
footerFrame.size.width = self.contentSize.width;
117+
_footerView.frame = footerFrame;
118+
[self addSubview:_footerView];
119+
}
120+
121+
// create array to hold it
122+
if (_thumbnails) {
123+
[_thumbnails removeAllObjects];
124+
[_thumbnails release], _thumbnails = nil;
125+
}
126+
_thumbnails = [[NSMutableArray alloc] initWithCapacity:_numberOfThumbnails];
127+
128+
// Get urls or images from the datasource
129+
CGFloat currentOffsetX = 0, currentOffsetY = 0;
130+
for (int counter = 0; counter < _numberOfThumbnails; counter++) {
131+
NSString *imageUrl = nil;
132+
UIImage *image = nil;
133+
NSString *imageTitle = nil;
134+
135+
if ([self.gridDataSource respondsToSelector:@selector(thumbnailGridView:imageAtIndex:)]) {
136+
image = [self.gridDataSource thumbnailGridView:self imageAtIndex:counter];
137+
}
138+
if ([self.gridDataSource respondsToSelector:@selector(thumbnailGridView:imageUrlAtIndex:)]) {
139+
imageUrl = [self.gridDataSource thumbnailGridView:self imageUrlAtIndex:counter];
140+
}
141+
if ([self.gridDataSource respondsToSelector:@selector(thumbnailGridView:titleForThumbnailAtIndex:)]) {
142+
imageTitle = [self.gridDataSource thumbnailGridView:self titleForThumbnailAtIndex:counter];
143+
}
144+
145+
// ImageView
146+
URLImageView *imageView = [[URLImageView alloc] initWithFrame:CGRectZero];
147+
[imageView.layer setBorderColor:[[UIColor colorWithWhite:0.7 alpha:1.0] CGColor]];
148+
[imageView.layer setBorderWidth:1.0];
149+
imageView.contentMode = UIViewContentModeScaleAspectFill;
150+
imageView.opaque = YES;
151+
imageView.clipsToBounds = YES;
152+
imageView.backgroundColor = self.backgroundColor;
153+
imageView.shouldShowActivityIndicator = YES;
154+
155+
//Title label
156+
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
157+
titleLabel.backgroundColor = self.backgroundColor;
158+
titleLabel.textColor = [UIColor colorWithRed:0.102 green:0.608 blue:0.906 alpha:1.0];
159+
titleLabel.textAlignment = UITextAlignmentCenter;
160+
titleLabel.font = [UIFont boldSystemFontOfSize:12.0];
161+
titleLabel.numberOfLines = 2;
162+
titleLabel.text = imageTitle;
163+
164+
// placement
165+
CGRect itemFrame = CGRectMake(currentOffsetX, currentOffsetY, _thumbnailWidth, _thumbnailHeight);
166+
if (imageTitle) {
167+
imageView.frame = CGRectMake(currentOffsetX + 10,
168+
currentOffsetY + 2,
169+
_thumbnailWidth - 20,
170+
_thumbnailHeight - 24);
171+
titleLabel.frame = CGRectMake(currentOffsetX + 2,
172+
currentOffsetY + _thumbnailHeight - 20,
173+
_thumbnailWidth - 4,
174+
20);
175+
}else {
176+
imageView.frame = CGRectInset(itemFrame, 2, 2);
177+
titleLabel.frame = CGRectZero;
178+
}
179+
180+
currentOffsetX += _thumbnailWidth;
181+
if (currentOffsetX >= self.bounds.size.width) {
182+
currentOffsetY += _thumbnailHeight;
183+
currentOffsetX = 0;
184+
}
185+
186+
[imageView addTarget:self withSelector:@selector(didTapImageThumbnail:)];
187+
188+
// add to the view
189+
[_thumbnails addObject:imageView];
190+
[self addSubview:imageView];
191+
[self addSubview:titleLabel];
192+
193+
if (image) {
194+
imageView.image = image;
195+
}
196+
if (imageUrl) {
197+
[imageView setRemoteImageUrl:imageUrl];
198+
}
199+
200+
[imageView release], imageView = nil;
201+
[titleLabel release], titleLabel = nil;
202+
}
203+
}
204+
205+
- (void)createStripe {
206+
// read number of images
207+
if ([self.gridDataSource respondsToSelector:@selector(numberOfThumbnailsInThumbnailGridView:)]) {
208+
_numberOfThumbnails = [self.gridDataSource numberOfThumbnailsInThumbnailGridView:self];
209+
210+
_numberOfThumbnails = _numberOfThumbnails < 0 ? 0 : _numberOfThumbnails;
211+
}
212+
213+
// calculate sizes for image and border
214+
_thumbnailBorderWidth = 0;
215+
216+
_thumbnailWidth = self.bounds.size.width / (CGFloat)_numberOfThumbnailsInRow;
217+
_thumbnailHeight = _thumbnailWidth;
218+
219+
// resize the scrollable area
220+
self.contentSize = CGSizeMake(_numberOfThumbnails * _thumbnailWidth,
221+
self.bounds.size.height);
222+
223+
// create array to hold it
224+
if (_thumbnails) {
225+
[_thumbnails removeAllObjects];
226+
[_thumbnails release], _thumbnails = nil;
227+
}
228+
_thumbnails = [[NSMutableArray alloc] initWithCapacity:_numberOfThumbnails];
229+
230+
// Get urls or images from the datasource
231+
CGFloat currentOffsetX = 0, currentOffsetY = 0;
232+
for (int counter = 0; counter < _numberOfThumbnails; counter++) {
233+
URLImageView *imageView = [[URLImageView alloc] initWithFrame:CGRectMake(currentOffsetX + 2,
234+
currentOffsetY + 2,
235+
_thumbnailWidth - 4,
236+
_thumbnailHeight - 4)];
237+
[imageView.layer setBorderColor:[[UIColor colorWithWhite:0.7 alpha:1.0] CGColor]];
238+
[imageView.layer setBorderWidth:1.0];
239+
imageView.backgroundColor = self.backgroundColor;
240+
imageView.shouldShowActivityIndicator = YES;
241+
currentOffsetX += _thumbnailWidth;
242+
243+
[imageView addTarget:self withSelector:@selector(didTapImageThumbnail:)];
244+
245+
if ([self.gridDataSource respondsToSelector:@selector(thumbnailGridView:imageAtIndex:)]) {
246+
imageView.image = [self.gridDataSource thumbnailGridView:self imageAtIndex:counter];
247+
}
248+
if ([self.gridDataSource respondsToSelector:@selector(thumbnailGridView:imageUrlAtIndex:)]) {
249+
[imageView setRemoteImageUrl:[self.gridDataSource thumbnailGridView:self imageUrlAtIndex:counter]];
250+
}
251+
// add to the view
252+
[_thumbnails addObject:imageView];
253+
[self addSubview:imageView];
254+
255+
[imageView release], imageView = nil;
256+
}
257+
}
258+
259+
260+
#pragma mark - Private Methods
261+
262+
- (void)didTapImageThumbnail:(URLImageView*)imageView {
263+
NSInteger index = [_thumbnails indexOfObject:imageView];
264+
265+
if ([self.gridDelegate respondsToSelector:@selector(thumbnailGridView:didSelectThumbnailAtIndex:)]) {
266+
[self.gridDelegate thumbnailGridView:self didSelectThumbnailAtIndex:index];
267+
}
268+
}
269+
270+
@end

0 commit comments

Comments
 (0)