зеркало из https://github.com/github/Rebel.git
Merge pull request #61 from ButterKit/fix-completion-block
Fix completion blocks being completely broken.
This commit is contained in:
Коммит
acc33ff438
|
@ -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
|
||||
|
|
Загрузка…
Ссылка в новой задаче