Skip to content
Browse files

Add the ability to clear the queue

Added two methods that allow for clearing of the queue. One
removes all the objects in the queue, while the other only
removes objects of a certain type.
  • Loading branch information...
1 parent 0bf9c54 commit 49bba1676dcd9244d8bc58ab3a17bb1687fbacfc Avi Itskovich committed Feb 17, 2011
Showing with 54 additions and 1 deletion.
  1. +6 −0 Classes/AIAnimationQueue.h
  2. +18 −0 Classes/AIAnimationQueue.m
  3. +1 −0 Classes/TestViewController.h
  4. +29 −1 Classes/TestViewController.m
View
6 Classes/AIAnimationQueue.h
@@ -38,6 +38,12 @@
- (void)addComputation:(void (^)(void))computation;
- (void)addBlock:(void (^)(void))block animation:(BOOL)animation;
+// Queue Management
+- (void)clear;
+- (void)removeObjectsOfType:(Class)classType;
+// Returns the number of animations still left in the queue
+// A currently running animation does not count as being the queue
+- (NSUInteger)count;
- (void)next;
@end
View
18 Classes/AIAnimationQueue.m
@@ -92,6 +92,24 @@ - (void)addBlock:(void (^)(void))block animation:(BOOL)animation {
#pragma mark -
#pragma mark Queue Management
+- (void)clear {
+ [queue removeAllObjects];
+}
+
+- (void)removeObjectsOfType:(Class)classType {
+ for (int i = 0; i < [queue count]; i++) {
+ id queueObject = [queue objectAtIndex:i];
+ if ([queueObject isKindOfClass:classType]) {
+ [queue removeObjectAtIndex:i];
+ i--;
+ }
+ }
+}
+
+- (NSUInteger)count {
+ return [queue count];
+}
+
- (void)next {
if ([queue count] > 0) {
AIQueueObject *animation = [[[queue objectAtIndex:0] retain] autorelease];
View
1 Classes/TestViewController.h
@@ -24,5 +24,6 @@
- (void)selectorWithContinuationTests;
- (void)blockTests;
- (void)blocksWithContinuationTests;
+- (void)objectRemovalTests;
@end
View
30 Classes/TestViewController.m
@@ -38,6 +38,7 @@ - (void)viewDidLoad {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
[self blockTests];
[self blocksWithContinuationTests];
+ [self objectRemovalTests];
# endif
}
@@ -172,8 +173,35 @@ - (void)blocksWithContinuationTests {
NSLog(@"Done 5");
}];
}
-#endif
+- (void)objectRemovalTests {
+ NSLog(@"=== Object Removal Tests ===");
+ AIAnimationQueue *animationQueue = [AIAnimationQueue sharedInstance];
+
+ for (int i = 0; i < 5; i++) {
+ [animationQueue addAnimation:^{
+ [UIView setAnimationDuration:5.0];
+ label.frame = label.frame;
+ }];
+ [animationQueue addComputation:^{
+ NSLog(@"Computation");
+ }];
+ }
+ [animationQueue clear];
+ NSAssert([animationQueue count] == 0, @"Clear didn't remove all parts");
+
+ for (int i = 0; i < 5; i++) {
+ [animationQueue addAnimation:^{
+ [UIView setAnimationDuration:5.0];
+ label.frame = CGRectMake(0, 0, 200, 200);
+ }];
+ [animationQueue addComputation:@selector(downContinuation) target:self];
+ }
+ [animationQueue removeObjectsOfType:[AISelectorQueueObject class]];
+ NSAssert([animationQueue count] == 5, @"Remove object types failed");
+}
+
+#endif
- (void)moveTo:(NSValue *)rect {
[UIView setAnimationDuration:2.0];
label.frame = [rect CGRectValue];

0 comments on commit 49bba16

Please sign in to comment.
Something went wrong with that request. Please try again.