Skip to content

Commit

Permalink
Allow code blocks for animations
Browse files Browse the repository at this point in the history
This addition allows code blocks for the animation of the cells.
  • Loading branch information
Wim Haanstra committed Oct 18, 2011
1 parent ee31dee commit 6ac2e6b
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 deletions.
7 changes: 6 additions & 1 deletion Classes/AQGridView.h
Expand Up @@ -51,14 +51,17 @@ typedef enum {
AQGridViewItemAnimationTop,
AQGridViewItemAnimationBottom,
AQGridViewItemAnimationNone,
AQGridViewItemAnimationPop
AQGridViewItemAnimationPop,
AQGridViewitemAnimationBlock
} AQGridViewItemAnimation;

typedef enum {
AQGridViewLayoutDirectionVertical,
AQGridViewLayoutDirectionHorizontal
} AQGridViewLayoutDirection;

typedef void (^AnimationBlock)(AQGridViewCell*);

@protocol AQGridViewDataSource;
@class AQGridView, AQGridViewData, AQGridViewUpdateInfo;

Expand Down Expand Up @@ -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;

Expand Down
21 changes: 20 additions & 1 deletion Classes/AQGridView.m
Expand Up @@ -974,7 +974,6 @@ - (void) endUpdateAnimations

self.animatingCells = [info animateCellUpdatesUsingVisibleContentRect: [self gridViewVisibleBounds]];


[_gridData release];
_gridData = [[info newGridViewData] retain];
if ( _selectedIndex != NSNotFound )
Expand Down Expand Up @@ -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];
Expand Down
5 changes: 5 additions & 0 deletions Classes/AQGridViewUpdateInfo.h
Expand Up @@ -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;
Expand Down
44 changes: 42 additions & 2 deletions Classes/AQGridViewUpdateInfo.m
Expand Up @@ -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
Expand Down Expand Up @@ -557,6 +575,10 @@ - (UIImageView *) animateDeletionForCell: (AQGridViewCell *) cell withAnimation:
imageView.center = center;
break;
}
case AQGridViewitemAnimationBlock:
{
break;
}

default:
break;
Expand All @@ -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];
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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]];
}
Expand Down
3 changes: 3 additions & 0 deletions Classes/AQGridViewUpdateItem.h
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
16 changes: 15 additions & 1 deletion Classes/AQGridViewUpdateItem.m
Expand Up @@ -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
{
Expand All @@ -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");
Expand Down

0 comments on commit 6ac2e6b

Please sign in to comment.