Created
November 4, 2011 23:06
-
-
Save nrj/1340724 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)insertCell:(UIView *)cell atIndex:(NSInteger)index animated:(BOOL)animated { | |
CGFloat duration = animated ? kHorizontalTableViewAnimationDuration : 0; | |
CGRect contentRect = [contentView frame]; | |
CGFloat colWidth = [dataSource horizontalTableView:self widthForCellAtIndex:index]; | |
CGFloat colHeight = [dataSource respondsToSelector:@selector(horizontalTableViewCellHeight:)] ? | |
[dataSource horizontalTableViewCellHeight:self] : contentRect.size.height; | |
CGFloat colSpacing = [dataSource respondsToSelector:@selector(horizontalTableViewCellSpacing:)] ? | |
[dataSource horizontalTableViewCellSpacing:self] : 0; | |
CGRect startRect = CGRectMake(0, 0, colWidth, colHeight); | |
if (index > [[contentView subviews] count]) { | |
[NSException raise:NSRangeException | |
format:@"Index %d is out of bounds [0 ... %d].", index, [[contentView subviews] count]]; | |
return; | |
} | |
if (index > 0) { | |
CGRect neighorRect = [[self viewForCellAtIndex:(index - 1)] frame]; | |
startRect.origin = neighorRect.origin; | |
} | |
CGFloat delay = 0.00; | |
CGFloat increment = 0.02; | |
[cell setFrame:startRect]; | |
[cell setAlpha:0.0]; | |
[contentView insertSubview:cell atIndex:([[contentView subviews] count] - index)]; | |
for (NSInteger i = ([[contentView subviews] count] - 1); i > index; i--) { | |
UIView *nextCell = [self viewForCellAtIndex:i]; | |
CGRect nextRect = [nextCell frame]; | |
[UIView animateWithDuration:duration | |
delay:delay | |
options:UIViewAnimationCurveEaseIn | |
animations:^(void) { | |
[nextCell setFrame:CGRectOffset(nextRect, colWidth + colSpacing, 0)]; | |
} | |
completion:^(BOOL finished) { | |
}]; | |
delay += increment; | |
} | |
[UIView animateWithDuration:kFrequencyDefaultAnimationDuration | |
delay:delay | |
options:UIViewAnimationCurveEaseIn | |
animations:^{ | |
[cell setFrame:CGRectOffset(startRect, colWidth + colSpacing, 0)]; | |
[cell setAlpha:1.0]; | |
} | |
completion:^(BOOL finished) { | |
[self setNeedsLayout]; | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment