Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Underlying UIScrollView loses scrolling behaviour when touched outside of TMQuiltViewCells #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions TMQuiltView/TMQuiltView/TMQuiltView.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ typedef enum {
@property (nonatomic, assign) id<TMQuiltViewDataSource> dataSource;
@property (nonatomic, assign) id<TMQuiltViewDelegate> delegate;

- (NSIndexPath*) indexPathForCell: (TMQuiltViewCell*) cell;

// Returns the cell if it's visible and indexPath is valid. Returns nil otherwise
- (TMQuiltViewCell *)cellAtIndexPath:(NSIndexPath*)indexPath;

Expand Down
27 changes: 27 additions & 0 deletions TMQuiltView/TMQuiltView/TMQuiltView.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,22 @@ - (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self) {
super.alwaysBounceVertical = YES;
self.backgroundColor = [UIColor blackColor];
[self addGestureRecognizer:self.tapGestureRecognizer];

_numberOfColumms = kTMQuiltViewDefaultColumns;
}
return self;
}

- (void)awakeFromNib {
super.alwaysBounceVertical = YES;
self.backgroundColor = [UIColor blackColor];
[self addGestureRecognizer:self.tapGestureRecognizer];

_numberOfColumms = kTMQuiltViewDefaultColumns;
}

- (void)setDelegate:(id<TMQuiltViewDelegate>)delegate {
[super setDelegate:delegate];
}
Expand Down Expand Up @@ -225,6 +235,21 @@ - (NSInteger)numberOfColumns {
return numberOfColumns;
}

- (NSIndexPath*) indexPathForCell: (TMQuiltViewCell*) cell {
__block NSIndexPath* path = nil;
for(int i = 0; i < _numberOfColumms; i++) {
NSMutableDictionary* indexPathsToViews = self.indexPathToViewByColumn[i];
[indexPathsToViews enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if (obj == cell) {
*stop = YES;
path = key;
}
}];
if (path) return path;
}
return nil;
}

/**
Excerpt from UITableView doc:

Expand Down Expand Up @@ -566,6 +591,8 @@ - (void)viewTapped:(UIGestureRecognizer *)recognizer {
while((indexPath = (NSIndexPath *)[displayedViewEnumerator nextObject])) {
TMQuiltViewCell *photoCell = [self.indexPathToViewByColumn[i] objectForKey:indexPath];
if (CGRectContainsPoint(photoCell.frame, tapPoint)) {
if (!photoCell.userInteractionEnabled) break;

photoCell.selected = YES;
if ([self.delegate respondsToSelector:@selector(quiltView:didSelectCellAtIndexPath:)]) {
[self.delegate quiltView:self didSelectCellAtIndexPath:indexPath];
Expand Down
2 changes: 2 additions & 0 deletions TMQuiltViewDemo/TMQuiltViewDemo/TMPhotoQuiltViewCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
//

#import <UIKit/UIKit.h>
#import "TMQuiltView.h"

@interface TMPhotoQuiltViewCell : TMQuiltViewCell

@property (nonatomic, retain) UIImageView *photoView;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UIImageView *starView;

@end
14 changes: 14 additions & 0 deletions TMQuiltViewDemo/TMQuiltViewDemo/TMPhotoQuiltViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ @implementation TMPhotoQuiltViewCell

@synthesize photoView = _photoView;
@synthesize titleLabel = _titleLabel;
@synthesize starView = _starView;

- (void)dealloc {
[_photoView release], _photoView = nil;
Expand All @@ -43,6 +44,17 @@ - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
return self;
}

- (UIImageView *)starView {
if (!_starView) {
_starView = [[UIImageView alloc] init];
_starView.contentMode = UIViewContentModeTopRight;
_starView.clipsToBounds = YES;
_starView.image = [UIImage imageNamed: @"star"];
[self addSubview:_starView];
}
return _starView;
}

- (UIImageView *)photoView {
if (!_photoView) {
_photoView = [[UIImageView alloc] init];
Expand All @@ -58,6 +70,7 @@ - (UILabel *)titleLabel {
_titleLabel = [[UILabel alloc] init];
_titleLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.font = [UIFont boldSystemFontOfSize: 14];
_titleLabel.textAlignment = UITextAlignmentCenter;
[self addSubview:_titleLabel];
}
Expand All @@ -68,6 +81,7 @@ - (void)layoutSubviews {
self.photoView.frame = CGRectInset(self.bounds, kTMPhotoQuiltViewMargin, kTMPhotoQuiltViewMargin);
self.titleLabel.frame = CGRectMake(kTMPhotoQuiltViewMargin, self.bounds.size.height - 20 - kTMPhotoQuiltViewMargin,
self.bounds.size.width - 2 * kTMPhotoQuiltViewMargin, 20);
self.starView.frame = CGRectInset(self.bounds, 5, 5);
}

@end