AnimOperationManager.m
6.6 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// AnimOperationManager.m
// presentAnimation
//
// Created by 许博 on 16/7/28.
// Copyright © 2016年 许博. All rights reserved.
// 新增动画管理类
#import "AnimOperationManager.h"
#import "AnimOperation.h"
//static NSInteger compareGiftID;
static NSString * compareUserId;
@interface AnimOperationManager ()
/// 队列1
@property (nonatomic,strong) NSOperationQueue *queue1;
/// 队列2
@property (nonatomic,strong) NSOperationQueue *queue2;
/// 操作缓存池
@property (nonatomic,strong) NSCache *operationCache;
/// 维护用户礼物信息
@property (nonatomic,strong) NSCache *userGigtInfos;
@end
@implementation AnimOperationManager
+ (instancetype)sharedManager
{
static AnimOperationManager *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[AnimOperationManager alloc] init];
});
return manager;
}
/// 动画操作 : 需要UserID和回调
- (void)animWithUserID:(NSString *)userID giftID:(NSInteger)giftID model:(GiftModel *)model finishedBlock:(void(^)(BOOL result))finishedBlock
{
// 在有用户礼物信息时
if ([self.userGigtInfos objectForKey:userID]) {
// 如果有操作缓存,则直接累加,不需要重新创建op
if ([self.operationCache objectForKey:userID]!=nil) {
AnimOperation *op = [self.operationCache objectForKey:userID];
op.presentView.giftCount = model.giftCount;
[op.presentView shakeNumberLabel];
return;
}
// 没有操作缓存,创建op
AnimOperation *op = [AnimOperation animOperationWithUserID:userID finishGiftCount:giftID model:model finishedBlock:^(BOOL result, NSInteger finishCount, NSInteger finishGiftCount)
{
// 回调
if (finishedBlock) {
finishedBlock(result);
}
// 将礼物信息数量存起来
[self.userGigtInfos setObject:@(finishCount) forKey:userID];
// 动画完成之后,要移除动画对应的操作
[self.operationCache removeObjectForKey:userID];
// 延时删除用户礼物信息
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.userGigtInfos removeObjectForKey:userID];
});
}];
// 注意:下面两句代码是和无用户礼物信息时不同的,其余的逻辑一样
// op.presentView.animCount = [[self.userGigtInfos objectForKey:userID] integerValue];
// op.model.giftCount = op.presentView.animCount + 1;
op.listView = self.parentView;
op.index = [userID integerValue] % 2;
// 将操作添加到缓存池
[self.operationCache setObject:op forKey:userID];
// 根据用户ID 控制显示的位置
if ([userID integerValue] % 2) {
if (op.model.giftCount != 0) {
op.presentView.frame = CGRectMake(-self.parentView.frame.size.width / 2, 300, self.parentView.frame.size.width / 2, 40);
op.presentView.originFrame = op.presentView.frame;
[self.queue1 addOperation:op];
}
}else {
if (op.model.giftCount != 0) {
op.presentView.frame = CGRectMake(-self.parentView.frame.size.width / 2, 240, self.parentView.frame.size.width / 2, 40);
op.presentView.originFrame = op.presentView.frame;
[self.queue2 addOperation:op];
}
}
}
// 在没有用户礼物信息时
else
{ // 如果有操作缓存,则直接累加,不需要重新创建op
if ([self.operationCache objectForKey:userID]!=nil) {
AnimOperation *op = [self.operationCache objectForKey:userID];
op.presentView.giftCount = model.giftCount;
[op.presentView shakeNumberLabel];
return;
}
AnimOperation *op = [AnimOperation animOperationWithUserID:userID finishGiftCount:giftID model:model finishedBlock:^(BOOL result, NSInteger finishCount, NSInteger finishGiftCount)
{
// 回调
if (finishedBlock) {
finishedBlock(result);
}
// 将礼物信息数量存起来
[self.userGigtInfos setObject:@(finishCount) forKey:userID];
// 动画完成之后,要移除动画对应的操作
[self.operationCache removeObjectForKey:userID];
// 延时删除用户礼物信息
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.userGigtInfos removeObjectForKey:userID];
});
}];
op.listView = self.parentView;
op.index = [userID integerValue] % 2;
// 将操作添加到缓存池
[self.operationCache setObject:op forKey:userID];
if ([userID integerValue] % 2) {
if (op.model.giftCount != 0) {
op.presentView.frame = CGRectMake(-self.parentView.frame.size.width / 2, 300, self.parentView.frame.size.width / 2, 40);
op.presentView.originFrame = op.presentView.frame;
[self.queue1 addOperation:op];
}
}else {
if (op.model.giftCount != 0) {
op.presentView.frame = CGRectMake(-self.parentView.frame.size.width / 2, 240, self.parentView.frame.size.width / 2, 40);
op.presentView.originFrame = op.presentView.frame;
[self.queue2 addOperation:op];
}
}
}
}
/// 取消上一次的动画操作 暂时没用到
- (void)cancelOperationWithLastUserID:(NSString *)userID {
// 当上次为空时就不执行取消操作 (第一次进入执行时才会为空)
if (userID!=nil) {
[[self.operationCache objectForKey:userID] cancel];
}
}
- (NSOperationQueue *)queue1
{
if (_queue1==nil) {
_queue1 = [[NSOperationQueue alloc] init];
_queue1.maxConcurrentOperationCount = 1;
}
return _queue1;
}
- (NSOperationQueue *)queue2
{
if (_queue2==nil) {
_queue2 = [[NSOperationQueue alloc] init];
_queue2.maxConcurrentOperationCount = 1;
}
return _queue2;
}
- (NSCache *)operationCache
{
if (_operationCache==nil) {
_operationCache = [[NSCache alloc] init];
}
return _operationCache;
}
- (NSCache *)userGigtInfos {
if (_userGigtInfos == nil) {
_userGigtInfos = [[NSCache alloc] init];
}
return _userGigtInfos;
}
@end