Last active
December 31, 2015 20:49
-
-
Save dlo/8042879 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't let you do the following: