From ee31dee75c3e50cb561382ae6d7aa6ff2e4b83c6 Mon Sep 17 00:00:00 2001 From: Wim Haanstra Date: Tue, 18 Oct 2011 11:05:39 +0200 Subject: [PATCH 1/2] Popup animation added Added a basic popup animation, which I use in one of my applications. --- Classes/AQGridView.h | 3 ++- Classes/AQGridViewUpdateInfo.m | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Classes/AQGridView.h b/Classes/AQGridView.h index f6ce0aa..63c27bf 100755 --- a/Classes/AQGridView.h +++ b/Classes/AQGridView.h @@ -50,7 +50,8 @@ typedef enum { AQGridViewItemAnimationLeft, AQGridViewItemAnimationTop, AQGridViewItemAnimationBottom, - AQGridViewItemAnimationNone + AQGridViewItemAnimationNone, + AQGridViewItemAnimationPop } AQGridViewItemAnimation; typedef enum { diff --git a/Classes/AQGridViewUpdateInfo.m b/Classes/AQGridViewUpdateInfo.m index d8f67aa..4ddbe24 100644 --- a/Classes/AQGridViewUpdateInfo.m +++ b/Classes/AQGridViewUpdateInfo.m @@ -521,6 +521,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; @@ -625,7 +626,16 @@ - (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; + } default: break; } @@ -680,6 +690,7 @@ - (void) animateReloadForCell: (AQGridViewCell *) newCell originalCell: (AQGridV switch ( animation ) { case AQGridViewItemAnimationFade: + case AQGridViewItemAnimationPop: default: break; // fade always happens From 6ac2e6bc69a9c2b5655506f6178c807a7991947f Mon Sep 17 00:00:00 2001 From: Wim Haanstra Date: Tue, 18 Oct 2011 19:24:59 +0200 Subject: [PATCH 2/2] Allow code blocks for animations This addition allows code blocks for the animation of the cells. --- Classes/AQGridView.h | 7 +++++- Classes/AQGridView.m | 21 +++++++++++++++- Classes/AQGridViewUpdateInfo.h | 5 ++++ Classes/AQGridViewUpdateInfo.m | 44 ++++++++++++++++++++++++++++++++-- Classes/AQGridViewUpdateItem.h | 3 +++ Classes/AQGridViewUpdateItem.m | 16 ++++++++++++- 6 files changed, 91 insertions(+), 5 deletions(-) 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");