|
| 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