Merge pull request #61 from ButterKit/fix-completion-block

Fix completion blocks being completely broken.
This commit is contained in:
Danny Greg 2013-01-09 10:50:01 -08:00
Родитель 0516388e88 8cac3a951d
Коммит acc33ff438
2 изменённых файлов: 28 добавлений и 10 удалений

Просмотреть файл

@ -8,24 +8,20 @@
#import "CAAnimation+RBLBlockAdditions.h"
#import <objc/runtime.h>
static void *RBLCAAnimationCompletionBlockAssociatedObjectKey = &RBLCAAnimationCompletionBlockAssociatedObjectKey;
@interface RBLCAAnimationDelegate : NSObject
@property (nonatomic, copy) void (^completion)(BOOL finished);
@end
@implementation CAAnimation (RBLBlockAdditions)
- (void)setRbl_completionBlock:(void (^)(BOOL))block {
RBLCAAnimationDelegate *delegateStub = [[RBLCAAnimationDelegate alloc] init];
self.delegate = delegateStub;
objc_setAssociatedObject(self, RBLCAAnimationCompletionBlockAssociatedObjectKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
RBLCAAnimationDelegate *stub = [[RBLCAAnimationDelegate alloc] init];
stub.completion = block;
self.delegate = stub;
}
- (void (^)(BOOL))rbl_completionBlock {
return objc_getAssociatedObject(self, RBLCAAnimationCompletionBlockAssociatedObjectKey);
return ([self.delegate isKindOfClass:RBLCAAnimationDelegate.class] ? [(RBLCAAnimationDelegate *)self.delegate completion] : nil);
}
@end
@ -33,7 +29,9 @@ static void *RBLCAAnimationCompletionBlockAssociatedObjectKey = &RBLCAAnimationC
@implementation RBLCAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag {
if (animation.rbl_completionBlock != nil) animation.rbl_completionBlock(flag);
if (self.completion != nil) {
self.completion(flag);
}
}
@end

Просмотреть файл

@ -32,4 +32,24 @@ it(@"Should not have a nil delegate", ^{
expect(animation.delegate).toNot.beNil();
});
it(@"Should fire once animation is completed", ^{
CALayer *layer = [CALayer layer];
CABasicAnimation *sampleAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
__block BOOL fired = NO;
sampleAnimation.rbl_completionBlock = ^(BOOL complete) {
fired = YES;
};
sampleAnimation.duration = 0.0;
[layer addAnimation:sampleAnimation forKey:@"opacity"];
layer.opacity = 1.f;
expect(fired).will.beTruthy();
});
it(@"Should return nil if no completion block has been set", ^{
CAAnimation *animation = [CAAnimation animation];
expect(animation.rbl_completionBlock).to.beNil();
});
SpecEnd