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

Added another basic animation #90

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion Classes/AQGridView.h
Expand Up @@ -50,14 +50,18 @@ typedef enum {
AQGridViewItemAnimationLeft,
AQGridViewItemAnimationTop,
AQGridViewItemAnimationBottom,
AQGridViewItemAnimationNone
AQGridViewItemAnimationNone,
AQGridViewItemAnimationPop,
AQGridViewitemAnimationBlock
} AQGridViewItemAnimation;

typedef enum {
AQGridViewLayoutDirectionVertical,
AQGridViewLayoutDirectionHorizontal
} AQGridViewLayoutDirection;

typedef void (^AnimationBlock)(AQGridViewCell*);

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

Expand Down Expand Up @@ -205,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
53 changes: 52 additions & 1 deletion 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 @@ -521,6 +539,7 @@ - (UIImageView *) animateDeletionForCell: (AQGridViewCell *) cell withAnimation:
// this is what we'll animate
switch ( animation )
{
case AQGridViewItemAnimationPop: // No action needed for the pop animation
case AQGridViewItemAnimationFade:
// nothing else left for the fade animation
break;
Expand Down Expand Up @@ -556,6 +575,10 @@ - (UIImageView *) animateDeletionForCell: (AQGridViewCell *) cell withAnimation:
imageView.center = center;
break;
}
case AQGridViewitemAnimationBlock:
{
break;
}

default:
break;
Expand All @@ -564,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 @@ -625,7 +656,21 @@ - (void) animateInsertionForCell: (AQGridViewCell *) cell withAnimation: (AQGrid
[itemsToSetBeforeAnimation setObject: [NSValue valueWithCGPoint: center] forKey: @"center"];
break;
}
case AQGridViewItemAnimationPop:
{
float percentageOfOriginalWidth = 10; // Make this higher or lower to start with a small or bigger picture
float width = (cellSize.width / 100) * percentageOfOriginalWidth; // 10 percent of original size
float inset = (cellSize.width / 2) - (width / 2);
CGRect newSize = CGRectInset(cell.frame, inset, inset);
[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 @@ -680,6 +725,7 @@ - (void) animateReloadForCell: (AQGridViewCell *) newCell originalCell: (AQGridV
switch ( animation )
{
case AQGridViewItemAnimationFade:
case AQGridViewItemAnimationPop:
default:
break; // fade always happens

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