Skip to content

Instantly share code, notes, and snippets.

@dlo
Last active December 31, 2015 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlo/8042879 to your computer and use it in GitHub Desktop.
Save dlo/8042879 to your computer and use it in GitHub Desktop.
//
// TMSerialAnimationQueue.h
// Orangina
//
// Created by Bryan Irace on 10/9/13.
// Copyright (c) 2013 Tumblr. All rights reserved.
//
/**
Contains boilerplate for performing animations serially, without blocking the main thread.
*/
@interface TMSerialAnimationQueue : NSObject
- (void)addAnimationBlock:(void (^)(void))animation;
@end
//
// TMSerialAnimationQueue.m
// Orangina
//
// Created by Bryan Irace on 10/9/13.
// Copyright (c) 2013 Tumblr. All rights reserved.
//
#import "TMSerialAnimationQueue.h"
@interface TMSerialAnimationQueue()
@property (nonatomic) dispatch_queue_t queue;
@property (nonatomic) dispatch_semaphore_t semaphore;
@end
@implementation TMSerialAnimationQueue
#pragma mark - NSObject
- (id)init {
if (self = [super init]) {
self.queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
}
return self;
}
#pragma mark - TMSerialAnimationQueue
- (void)addAnimationBlock:(void (^)(void))animation {
if (!animation) {
return;
}
dispatch_async(self.queue, ^{
self.semaphore = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
animation();
dispatch_semaphore_signal(self.semaphore);
});
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
self.semaphore = nil;
});
}
@end
@irace
Copy link

irace commented Dec 19, 2013

This doesn't let you do the following:

[animationQueue addAnimationBlock:^{
    [UIView animateWithDuration:TMAnimationDuration
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         label.alpha = 1;
                         label.frameY -= animationDistance;

                     } completion:^(BOOL finished) {
                         [UIView animateWithDuration:TMAnimationDuration
                                               delay:TMAnimationDelay
                                             options:UIViewAnimationOptionCurveEaseInOut animations:^{
                                                 label.alpha = 0;
                                                 label.frameY += animationDistance;

                                             } completion:^(BOOL finished2) {
                                                 [label removeFromSuperview];

                                                 [weakSelf.animationQueue finish];
                                             }];
                     }];
}];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment