Skip to content

Commit a22fd10

Browse files
author
Ernesto Rivera
committed
Allow executing a block while temporarily overriding default animations
1 parent a618212 commit a22fd10

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

Source/AMBTableViewController.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@
8080
/// Insert section/row animation. By default `UITableViewRowAnimationAutomatic`.
8181
@property (nonatomic) UITableViewRowAnimation removeAnimation;
8282

83+
/// Execute a block while temporarily overriding reload, insert and remove animations.
84+
/// @param changes The block to be executed.
85+
/// @param animation The animation to be used while executing the changes.
86+
- (void)applyChanges:(void (^)(void))changes
87+
withAnimation:(UITableViewRowAnimation)animation;
88+
89+
/// Execute a block while temporarily overriding reload, insert and remove animations.
90+
/// @param changes The block to be executed.
91+
/// @param reloadAnimation The animation to be used while reloading sections/rows.
92+
/// @param insertAnimation The animation to be used while inserting sections/rows.
93+
/// @param removeAnimation The animation to be used while removing sections/rows.
94+
- (void)applyChanges:(void (^)(void))changes
95+
withReloadAnimation:(UITableViewRowAnimation)reloadAnimation
96+
insertAnimation:(UITableViewRowAnimation)insertAnimation
97+
removeAnimation:(UITableViewRowAnimation)removeAnimation;
98+
8399
/// @name Convenience Methods
84100

85101
/// Trigger [AMBTableViewSection update] on all sections.

Source/AMBTableViewController.m

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
@implementation AMBTableViewController
2424
{
2525
NSMutableArray * _mutableSections;
26+
BOOL _useCustomAnimations;
27+
UITableViewRowAnimation _customReloadAnimation;
28+
UITableViewRowAnimation _customInsertAnimation;
29+
UITableViewRowAnimation _customRemoveAnimation;
2630
}
2731

2832
@dynamic sections;
@@ -126,6 +130,46 @@ - (void)replaceSectionAtIndex:(NSUInteger)index
126130
[section reload];
127131
}
128132

133+
#pragma mark - Configuring animations
134+
135+
- (UITableViewRowAnimation)reloadAnimation
136+
{
137+
return _useCustomAnimations ? _customReloadAnimation : _reloadAnimation;
138+
}
139+
140+
- (UITableViewRowAnimation)insertAnimation
141+
{
142+
return _useCustomAnimations ? _customInsertAnimation : _insertAnimation;
143+
}
144+
145+
- (UITableViewRowAnimation)removeAnimation
146+
{
147+
return _useCustomAnimations ? _customRemoveAnimation : _removeAnimation;
148+
}
149+
150+
- (void)applyChanges:(void (^)(void))changes
151+
withAnimation:(UITableViewRowAnimation)animation
152+
{
153+
[self applyChanges:changes
154+
withReloadAnimation:animation
155+
insertAnimation:animation
156+
removeAnimation:animation];
157+
}
158+
159+
- (void)applyChanges:(void (^)(void))changes
160+
withReloadAnimation:(UITableViewRowAnimation)reloadAnimation
161+
insertAnimation:(UITableViewRowAnimation)insertAnimation
162+
removeAnimation:(UITableViewRowAnimation)removeAnimation
163+
{
164+
_useCustomAnimations = YES;
165+
_customReloadAnimation = reloadAnimation;
166+
_customInsertAnimation = insertAnimation;
167+
_customRemoveAnimation = removeAnimation;
168+
169+
changes();
170+
171+
_useCustomAnimations = NO;
172+
}
129173

130174
#pragma mark - Convenience methods
131175

0 commit comments

Comments
 (0)