YHAnimationGroup.m
2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// YHAnimationGroup.m
// YHAnimationDemo
//
// Created by gaoqiang xu on 2/9/15.
// Copyright (c) 2015 gaoqiang. All rights reserved.
//
#import "YHAnimationGroup.h"
#import "NSObject+YHAnimationGroupInternal.h"
@interface YHAnimationGroup ()
@property (strong, nonatomic) NSMutableSet *remainingAnimations;
@property (strong, nonatomic) NSMapTable *animationsFinished;
@end
@implementation YHAnimationGroup
- (instancetype)init {
self = [super init];
if (self) {
_remainingAnimations = [NSMutableSet set];
_animationsFinished = [NSMapTable strongToStrongObjectsMapTable];
}
return self;
}
- (void)addAnimation:(POPAnimation *)animation {
animation.completionBlock = ^(POPAnimation *animation, BOOL finished) {
for (YHAnimationGroup *group in [NSObject yh_animationGroups]) {
[group removeAnimation:animation finished:finished];
}
};
[self.animationsFinished setObject:@(NO) forKey:animation];
[self.remainingAnimations addObject:animation];
}
- (void)removeAnimation:(POPAnimation *)animation finished:(BOOL)finished {
if (![self.remainingAnimations containsObject:animation]) {
return;
}
[self.animationsFinished setObject:@(finished) forKey:animation];
[self.remainingAnimations removeObject:animation];
if ([self.remainingAnimations count] < 1) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self.completionBlock) {
self.completionBlock([self isFinished]);
}
[self.animationsFinished removeAllObjects];
});
}
}
#pragma mark - Properties
- (BOOL)isFinished {
for (POPAnimation *animation in self.animationsFinished) {
if (![[self.animationsFinished objectForKey:animation] boolValue]) {
return NO;
}
}
return YES;
}
@end
@implementation NSObject (YHAnimationGroup)
+ (void)pop_animate:(void (^)(void))animations completion:(void (^)(BOOL))completion {
YHAnimationGroup *group = [[YHAnimationGroup alloc] init];
[[self yh_animationGroups] addObject:group];
__weak YHAnimationGroup *weakGroup = group;
group.completionBlock = ^(BOOL finished) {
YHAnimationGroup *strongGroup = weakGroup;
[[self yh_animationGroups] removeObject:strongGroup];
if (completion) {
completion(finished);
}
};
[self yh_setActiveAnimationGroup:group];
if (animations) {
animations();
}
[self yh_setActiveAnimationGroup:nil];
}
@end