Skip to content

Commit

Permalink
Added properties settings back in.
Browse files Browse the repository at this point in the history
  • Loading branch information
kirbyt committed Mar 7, 2011
1 parent 942d613 commit f10da22
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 39 deletions.
7 changes: 5 additions & 2 deletions src/KTPhotoBrowser/KTThumbView.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@

@class KTThumbsViewController;

@interface KTThumbView : UIButton {
@interface KTThumbView : UIButton
{
@private
KTThumbsViewController *controller_;
}

@property (nonatomic, assign) KTThumbsViewController *controller;

- (id)initWithFrame:(CGRect)frame andHasBorder:(BOOL)hasBorder;
- (id)initWithFrame:(CGRect)frame;
- (void)setThumbImage:(UIImage *)newImage;
- (void)setHasBorder:(BOOL)hasBorder;

@end

28 changes: 19 additions & 9 deletions src/KTPhotoBrowser/KTThumbView.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,19 @@ @implementation KTThumbView

@synthesize controller = controller_;

- (void)dealloc {
- (void)dealloc
{
[super dealloc];
}

- (id)initWithFrame:(CGRect)frame andHasBorder:(BOOL)hasBorder {
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {

[self addTarget:self
action:@selector(didTouch:)
forControlEvents:UIControlEventTouchUpInside];

if (hasBorder) {
self.layer.borderColor = [UIColor colorWithWhite:0.85 alpha:1.0].CGColor;
self.layer.borderWidth = 1;
}

[self setClipsToBounds:YES];

// If the thumbnail needs to be scaled, it should mantain its aspect
Expand All @@ -40,14 +37,27 @@ - (id)initWithFrame:(CGRect)frame andHasBorder:(BOOL)hasBorder {
return self;
}

- (void)didTouch:(id)sender {
- (void)didTouch:(id)sender
{
if (controller_) {
[controller_ didSelectThumbAtIndex:[self tag]];
}
}

- (void)setThumbImage:(UIImage *)newImage {
- (void)setThumbImage:(UIImage *)newImage
{
[self setImage:newImage forState:UIControlStateNormal];
}

- (void)setHasBorder:(BOOL)hasBorder
{
if (hasBorder) {
self.layer.borderColor = [UIColor colorWithWhite:0.85 alpha:1.0].CGColor;
self.layer.borderWidth = 1;
} else {
self.layer.borderColor = nil;
}
}


@end
10 changes: 7 additions & 3 deletions src/KTPhotoBrowser/KTThumbsView.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
@private
id <KTThumbsViewDataSource> dataSource_;
KTThumbsViewController *controller_;
BOOL thumbsHaveBorder_;
NSInteger thumbsPerRow_;
CGSize thumbSize_;

NSMutableSet *reusableThumbViews_;

// We use the following ivars to keep track of
Expand All @@ -27,6 +31,9 @@

@property (nonatomic, assign) id<KTThumbsViewDataSource> dataSource;
@property (nonatomic, assign) KTThumbsViewController *controller;
@property (nonatomic, assign) BOOL thumbsHaveBorder;
@property (nonatomic, assign) NSInteger thumbsPerRow;
@property (nonatomic, assign) CGSize thumbSize;

- (KTThumbView *)dequeueReusableThumbView;
- (void)reloadData;
Expand All @@ -37,8 +44,5 @@
@required
- (NSInteger)thumbsViewNumberOfThumbs:(KTThumbsView *)thumbsView;
- (KTThumbView *)thumbsView:(KTThumbsView *)thumbsView thumbForIndex:(NSInteger)index;
@optional
- (CGSize)thumbsViewThumbSize:(KTThumbsView *)thumbsView;
- (NSInteger)thumbsViewThumbsPerRow:(KTThumbsView *)thumbsView;

@end
38 changes: 19 additions & 19 deletions src/KTPhotoBrowser/KTThumbsView.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ @implementation KTThumbsView

@synthesize dataSource = dataSource_;
@synthesize controller = controller_;
@synthesize thumbsHaveBorder = thumbsHaveBorder_;
@synthesize thumbsPerRow = thumbsPerRow_;
@synthesize thumbSize = thumbSize_;

- (void)dealloc
{
Expand All @@ -26,6 +29,11 @@ - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Set default values.
thumbsHaveBorder_ = YES;
thumbsPerRow_ = NSIntegerMin; // Forces caluation because on view size.
thumbSize_ = CGSizeMake(75, 75);

// We keep a collection of reusable thumbnail
// views. This improves performance by not
// requiring a create view each and every time.
Expand Down Expand Up @@ -127,36 +135,26 @@ - (void)layoutSubviews
// Do a bunch of math to determine which rows and colums
// are visible.

int thumbnailWidth = 75; // Default thumbnail size.
int thumbnailHeight = 75; // Default thumbnail size.
if ([dataSource_ respondsToSelector:@selector(thumbsViewThumbSize:)]) {
CGSize customThumbSize = [dataSource_ thumbsViewThumbSize:self];
thumbnailWidth = customThumbSize.width;
thumbnailHeight = customThumbSize.height;
}

int itemsPerRow;
if ([dataSource_ respondsToSelector:@selector(thumbsViewThumbsPerRow:)]) {
itemsPerRow = [dataSource_ thumbsViewThumbsPerRow:self];
} else { // Figure it out.
itemsPerRow = floor(viewWidth / thumbnailWidth);
int itemsPerRow = thumbsPerRow_;
if (itemsPerRow == NSIntegerMin) {
itemsPerRow = floor(viewWidth / thumbSize_.width);
}

// Ensure a minimum of space between images.
int minimumSpace = 5;
if (viewWidth - itemsPerRow * thumbnailWidth < minimumSpace) {
if (viewWidth - itemsPerRow * thumbSize_.width < minimumSpace) {
itemsPerRow--;
}

if (itemsPerRow < 1) itemsPerRow = 1; // Ensure at least one per row.

int spaceWidth = round((viewWidth - thumbnailWidth * itemsPerRow) / (itemsPerRow + 1));
int spaceWidth = round((viewWidth - thumbSize_.width * itemsPerRow) / (itemsPerRow + 1));
int spaceHeight = spaceWidth;

// Calculate content size.
int thumbCount = [dataSource_ thumbsViewNumberOfThumbs:self];
int rowCount = ceil(thumbCount / (float)itemsPerRow);
int rowHeight = thumbnailHeight + spaceHeight;
int rowHeight = thumbSize_.height + spaceHeight;
CGSize contentSize = CGSizeMake(viewWidth, (rowHeight * rowCount + spaceHeight));
[self setContentSize:contentSize];

Expand All @@ -180,23 +178,25 @@ - (void)layoutSubviews
KTThumbView *thumbView = [dataSource_ thumbsView:self thumbForIndex:index];

// Set the frame so the view is inserted into the correct position.
CGRect newFrame = CGRectMake(x, y, thumbnailWidth, thumbnailHeight);
CGRect newFrame = CGRectMake(x, y, thumbSize_.width, thumbSize_.height);
[thumbView setFrame:newFrame];

// Store the current index so the thumb view can
// find it later.
[thumbView setTag:index];

[thumbView setHasBorder:thumbsHaveBorder_];

[self addSubview:thumbView];
}

// Adjust the position.
if ( (index+1) % itemsPerRow == 0) {
// Start new row.
x = spaceWidth;
y += thumbnailHeight + spaceHeight;
y += thumbSize_.height + spaceHeight;
} else {
x += thumbnailWidth + spaceWidth;
x += thumbSize_.width + spaceWidth;
}
}

Expand Down
20 changes: 14 additions & 6 deletions src/KTPhotoBrowser/KTThumbsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ - (void)loadView {
[scrollView setAlwaysBounceVertical:YES];
[scrollView setBackgroundColor:[UIColor whiteColor]];

if ([dataSource_ respondsToSelector:@selector(thumbsHaveBorder)]) {
[scrollView setThumbsHaveBorder:[dataSource_ thumbsHaveBorder]];
}

if ([dataSource_ respondsToSelector:@selector(thumbSize)]) {
[scrollView setThumbSize:[dataSource_ thumbSize]];
}

if ([dataSource_ respondsToSelector:@selector(thumbsPerRow)]) {
[scrollView setThumbsPerRow:[dataSource_ thumbsPerRow]];
}


// Set main view to the scroll view.
[self setView:scrollView];

Expand Down Expand Up @@ -121,12 +134,7 @@ - (KTThumbView *)thumbsView:(KTThumbsView *)thumbsView thumbForIndex:(NSInteger)
{
KTThumbView *thumbView = [thumbsView dequeueReusableThumbView];
if (!thumbView) {
BOOL hasBorder = YES;
if ([dataSource_ respondsToSelector:@selector(thumbsHaveBorder)]) {
hasBorder = [dataSource_ thumbsHaveBorder];
}

thumbView = [[[KTThumbView alloc] initWithFrame:CGRectZero andHasBorder:hasBorder] autorelease];
thumbView = [[[KTThumbView alloc] initWithFrame:CGRectZero] autorelease];
[thumbView setController:self];
}

Expand Down

0 comments on commit f10da22

Please sign in to comment.