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

Fixes #7 #10

Open
wants to merge 4 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: 1 addition & 1 deletion TMQuiltView/TMQuiltView/TMQuiltView.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ typedef enum {

@end

@interface TMQuiltView : UIScrollView
@interface TMQuiltView : UIScrollView <UIGestureRecognizerDelegate>

@property (nonatomic, assign) id<TMQuiltViewDataSource> dataSource;
@property (nonatomic, assign) id<TMQuiltViewDelegate> delegate;
Expand Down
57 changes: 52 additions & 5 deletions TMQuiltView/TMQuiltView/TMQuiltView.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

NSString *const kDefaultReusableIdentifier = @"kTMQuiltViewDefaultReusableIdentifier";

@interface TMQuiltView()
@interface TMQuiltView(){
BOOL _videoPlayedInFullScreen;
}

@property (nonatomic, readonly, retain) NSMutableSet *indexPaths;
@property (nonatomic, readonly, retain) NSMutableDictionary *reusableViewsDictionary;
Expand Down Expand Up @@ -126,9 +128,24 @@ - (id)initWithFrame:(CGRect)frame
super.alwaysBounceVertical = YES;
[self addGestureRecognizer:self.tapGestureRecognizer];
_numberOfColumms = kTMQuiltViewDefaultColumns;
_videoPlayedInFullScreen = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoInFullScreenDidStart:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoInFullScreenDidEnd:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];

}
return self;
}

- (void)videoInFullScreenDidStart:(id)notification
{
_videoPlayedInFullScreen = YES;
}

- (void)videoInFullScreenDidEnd:(id)notification
{
_videoPlayedInFullScreen = NO;
}


- (void)setDelegate:(id<TMQuiltViewDelegate>)delegate {
[super setDelegate:delegate];
Expand Down Expand Up @@ -397,7 +414,7 @@ - (CGRect)rectForCellAtIndex:(int)index column:(int)column {
[self cellWidth], height);
}

- (void)layoutSubviews {
- (void)layoutSubviews {
[super layoutSubviews];

self.contentSize = CGSizeMake(self.bounds.size.width, self.contentSize.height);
Expand Down Expand Up @@ -449,8 +466,7 @@ - (void)layoutSubviews {
}
(*bottom)++;
}



// Add a new cell to the top if our top cell is below the top of the visible area (and not the first cell)
while ((*top > 0) && [TMQuiltView isRect:[self rectForCellAtIndex:*top column:i] entirelyInOrBelowScrollView:self]) {
if ([TMQuiltView isRect:[self rectForCellAtIndex:*top - 1 column:i] partiallyInScrollView:self]) {
Expand All @@ -466,8 +482,10 @@ - (void)layoutSubviews {
// Harvest any any views that have moved off screen and add them to the reuse pool
for (NSIndexPath* indexPath in [indexPathToView allKeys]) {
TMQuiltViewCell *view = [indexPathToView objectForKey:indexPath];

if (![TMQuiltView isRect:view.frame partiallyInScrollView:self]) { // Rect intersection?
[indexPathToView removeObjectForKey:indexPath];

// Limit the size on the reuse pool
if ([[self reusableViewsWithReuseIdentifier:view.reuseIdentifier] count] < 10) {
[[self reusableViewsWithReuseIdentifier:view.reuseIdentifier] addObject:view];
Expand All @@ -493,13 +511,34 @@ - (void)layoutSubviews {
break;
}
}

// The last cell in a column might have been harvested after our scroll view bounce.
// Here we check if the last cell's frame is partially in our scroll view
if (*bottom == [indexPaths count] - 1 && [TMQuiltView isRect:[self rectForCellAtIndex:*bottom column:i] partiallyInScrollView:self]){
NSIndexPath *indexPath = [indexPaths objectAtIndex:*bottom];
// We check, if the last cell has actually been harvested...
if ([indexPathToView objectForKey:indexPath] == nil) {
// ... and if so, we add it back
UIView* cell = [self.dataSource quiltView:self cellAtIndexPath:indexPath];
[self addSubview:cell];
[indexPathToView setObject:cell forKey:indexPath];
}
}
}
}

- (void)setFrame:(CGRect)frame {
// If we have an MPMoviePlayer that is just exiting fullscreen,
// removing it's superview from the view hierarchy would cause
// the minimalize animation to be broken
if (_videoPlayedInFullScreen) {
_videoPlayedInFullScreen = NO;
return;
}

[super setFrame:frame];

// We need to recompute the cell tops because their width is
// We need to recompute the cell tops because their width is
// based on the bounding width, and their height is generally based
// on their width.
[self resetView];
Expand Down Expand Up @@ -548,6 +587,7 @@ + (BOOL)isRect:(CGRect)rect partiallyInScrollView:(UIScrollView *)scrollView {
- (UITapGestureRecognizer *)tapGestureRecognizer {
if (!_tapGestureRecognizer) {
_tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
_tapGestureRecognizer.delegate = self;
}
return _tapGestureRecognizer;
}
Expand Down Expand Up @@ -577,6 +617,13 @@ - (void)viewTapped:(UIGestureRecognizer *)recognizer {

}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UIControl class]]) {
// we touched a button, slider, or other UIControl
return NO; // ignore the touch
}
return YES; // handle the touch
}


@end