diff --git a/Classes/AQGridView.h b/Classes/AQGridView.h index 63c27bf..7e9e6a9 100755 --- a/Classes/AQGridView.h +++ b/Classes/AQGridView.h @@ -51,7 +51,8 @@ typedef enum { AQGridViewItemAnimationTop, AQGridViewItemAnimationBottom, AQGridViewItemAnimationNone, - AQGridViewItemAnimationPop + AQGridViewItemAnimationPop, + AQGridViewitemAnimationBlock } AQGridViewItemAnimation; typedef enum { @@ -59,6 +60,8 @@ typedef enum { AQGridViewLayoutDirectionHorizontal } AQGridViewLayoutDirection; +typedef void (^AnimationBlock)(AQGridViewCell*); + @protocol AQGridViewDataSource; @class AQGridView, AQGridViewData, AQGridViewUpdateInfo; @@ -206,6 +209,8 @@ extern NSString * const AQGridViewSelectionDidChangeNotification; - (void) endUpdates; // only call insert/delete/reload calls inside an update block. - (void) insertItemsAtIndices: (NSIndexSet *) indices withAnimation: (AQGridViewItemAnimation) animation; +- (void) insertItemsAtIndices:(NSIndexSet *)indices withAnimationBlock:(AnimationBlock) animationBlock; + - (void) deleteItemsAtIndices: (NSIndexSet *) indices withAnimation: (AQGridViewItemAnimation) animation; - (void) reloadItemsAtIndices: (NSIndexSet *) indices withAnimation: (AQGridViewItemAnimation) animation; diff --git a/Classes/AQGridView.m b/Classes/AQGridView.m index ebeac7e..80f35e1 100755 --- a/Classes/AQGridView.m +++ b/Classes/AQGridView.m @@ -974,7 +974,6 @@ - (void) endUpdateAnimations self.animatingCells = [info animateCellUpdatesUsingVisibleContentRect: [self gridViewVisibleBounds]]; - [_gridData release]; _gridData = [[info newGridViewData] retain]; if ( _selectedIndex != NSNotFound ) @@ -1029,6 +1028,26 @@ - (void) _updateItemsAtIndices: (NSIndexSet *) indices updateAction: (AQGridView [self endUpdateAnimations]; } + + +- (void) _updateItemsAtIndices:(NSIndexSet*) indices updateAction:(AQGridViewUpdateAction) action withAnimationBlock:(AnimationBlock) animationBlock +{ + BOOL needsAnimationSetup = ([_updateInfoStack count] <= _animationCount); + + if (needsAnimationSetup) + [self setupUpdateAnimations]; + + [[_updateInfoStack lastObject] updateItemsAtIndices: indices updateAction: action withAnimationBlock:animationBlock]; + + if (needsAnimationSetup) + [self endUpdateAnimations]; +} + +- (void) insertItemsAtIndices:(NSIndexSet *)indices withAnimationBlock:(AnimationBlock) animationBlock +{ + [self _updateItemsAtIndices:indices updateAction:AQGridViewUpdateActionInsert withAnimationBlock:animationBlock]; +} + - (void) insertItemsAtIndices: (NSIndexSet *) indices withAnimation: (AQGridViewItemAnimation) animation { [self _updateItemsAtIndices: indices updateAction: AQGridViewUpdateActionInsert withAnimation: animation]; diff --git a/Classes/AQGridViewUpdateInfo.h b/Classes/AQGridViewUpdateInfo.h index 795b950..8eef964 100644 --- a/Classes/AQGridViewUpdateInfo.h +++ b/Classes/AQGridViewUpdateInfo.h @@ -82,6 +82,11 @@ - (void) updateItemsAtIndices: (NSIndexSet *) indices updateAction: (AQGridViewUpdateAction) action withAnimation: (AQGridViewItemAnimation) animation; + +- (void) updateItemsAtIndices: (NSIndexSet *) indices + updateAction: (AQGridViewUpdateAction) action + withAnimationBlock: (AnimationBlock) animationBlock; + - (void) moveItemAtIndex: (NSUInteger) index toIndex: (NSUInteger) index withAnimation: (AQGridViewItemAnimation) animation; diff --git a/Classes/AQGridViewUpdateInfo.m b/Classes/AQGridViewUpdateInfo.m index 4ddbe24..def567a 100644 --- a/Classes/AQGridViewUpdateInfo.m +++ b/Classes/AQGridViewUpdateInfo.m @@ -126,6 +126,24 @@ - (void) updateItemsAtIndices: (NSIndexSet *) indices } } +- (void) updateItemsAtIndices: (NSIndexSet *) indices + updateAction: (AQGridViewUpdateAction) action + withAnimationBlock: (AnimationBlock) animationBlock +{ + NSMutableArray * array = [self updateItemArrayForAction: action]; + NSUInteger i = [indices firstIndex]; + while ( i != NSNotFound ) + { + AQGridViewUpdateItem * item = [[AQGridViewUpdateItem alloc] initWithIndex: i + action: action + animationBlock:animationBlock]; + [array addObject: item]; + [item release]; + + i = [indices indexGreaterThanIndex: i]; + } +} + - (void) moveItemAtIndex: (NSUInteger) index toIndex: (NSUInteger) newIndex withAnimation: (AQGridViewItemAnimation) animation @@ -557,6 +575,10 @@ - (UIImageView *) animateDeletionForCell: (AQGridViewCell *) cell withAnimation: imageView.center = center; break; } + case AQGridViewitemAnimationBlock: + { + break; + } default: break; @@ -565,6 +587,14 @@ - (UIImageView *) animateDeletionForCell: (AQGridViewCell *) cell withAnimation: return ( imageView ); } +- (void) animateInsertionForCell:(AQGridViewCell*) cell withAnimationBlock:(AnimationBlock) animationBlock +{ + [_gridView addSubview: cell]; + [UIView setAnimationsEnabled: YES]; + + animationBlock(cell); +} + - (void) animateInsertionForCell: (AQGridViewCell *) cell withAnimation: (AQGridViewItemAnimation) animation { [UIView setAnimationsEnabled: NO]; @@ -635,7 +665,12 @@ - (void) animateInsertionForCell: (AQGridViewCell *) cell withAnimation: (AQGrid [itemsToAnimate setObject:[NSValue valueWithCGRect:cell.frame] forKey: @"frame"]; [itemsToSetBeforeAnimation setObject: [NSValue valueWithCGRect:newSize] forKey: @"frame"]; break; - } + } + case AQGridViewitemAnimationBlock: + { + + break; + } default: break; } @@ -896,7 +931,12 @@ - (NSSet *) animateCellUpdatesUsingVisibleContentRect: (CGRect) contentRect AQGridViewCell * cell = [_gridView createPreparedCellForIndex: item.index usingGridData: _newGridData]; if ( cell != nil ) { - [self animateInsertionForCell: cell withAnimation: item.animation]; + if (item.animation == AQGridViewitemAnimationBlock) + { + [self animateInsertionForCell:cell withAnimationBlock:item.animationBlock]; + } + else + [self animateInsertionForCell: cell withAnimation: item.animation]; [_gridView delegateWillDisplayCell: cell atIndex: item.index]; [newVisibleCells addObject: [AQGridViewAnimatorItem itemWithView: cell index: item.index]]; } diff --git a/Classes/AQGridViewUpdateItem.h b/Classes/AQGridViewUpdateItem.h index d9a249a..a51a5c1 100644 --- a/Classes/AQGridViewUpdateItem.h +++ b/Classes/AQGridViewUpdateItem.h @@ -50,10 +50,12 @@ typedef enum { NSUInteger _newIndex; AQGridViewUpdateAction _action; AQGridViewItemAnimation _animation; + AnimationBlock _animationBlock; NSInteger _offset; } - (id) initWithIndex: (NSUInteger) index action: (AQGridViewUpdateAction) action animation: (AQGridViewItemAnimation) animation; +- (id) initWithIndex:(NSUInteger)index action:(AQGridViewUpdateAction)action animationBlock:(AnimationBlock)animationBlock; - (NSComparisonResult) compare: (AQGridViewUpdateItem *) other; - (NSComparisonResult) inverseCompare: (AQGridViewUpdateItem *) other; @@ -62,6 +64,7 @@ typedef enum { @property (nonatomic) NSUInteger newIndex; // only valid for AQGridViewUpdateActionMove @property (nonatomic, readonly) AQGridViewUpdateAction action; @property (nonatomic, readonly) AQGridViewItemAnimation animation; +@property (nonatomic, readonly) AnimationBlock animationBlock; // this is an offset to apply to the index, due to other changes in the list which occurred since this index was chosen @property (nonatomic) NSInteger offset; diff --git a/Classes/AQGridViewUpdateItem.m b/Classes/AQGridViewUpdateItem.m index 3088d52..9aa3944 100644 --- a/Classes/AQGridViewUpdateItem.m +++ b/Classes/AQGridViewUpdateItem.m @@ -38,7 +38,7 @@ @implementation AQGridViewUpdateItem -@synthesize originalIndex=_index, newIndex=_newIndex, action=_action, animation=_animation, offset=_offset; +@synthesize originalIndex=_index, newIndex=_newIndex, action=_action, animation=_animation, offset=_offset, animationBlock = _animationBlock; - (id) initWithIndex: (NSUInteger) index action: (AQGridViewUpdateAction) action animation: (AQGridViewItemAnimation) animation { @@ -53,6 +53,20 @@ - (id) initWithIndex: (NSUInteger) index action: (AQGridViewUpdateAction) action return ( self ); } +- (id) initWithIndex:(NSUInteger)index action:(AQGridViewUpdateAction)action animationBlock:(AnimationBlock)animationBlock +{ + self = [super init]; + if ( self == nil ) + return ( nil ); + + _index = index; + _action = action; + _animation = AQGridViewitemAnimationBlock; + _animationBlock = animationBlock; + + return ( self ); +} + - (void) setNewIndex: (NSUInteger) value { NSAssert(self.action == AQGridViewUpdateActionMove, @"newIndex set on a non-move update item");