主要用到NSOperation和NSOperationQueue实现动画队列
主要分三个类
animationManager :
#import "YGPreGiftAnimationManager.h"
#import "YGPreGiftAnimationOperation.h"
@interface YGPreGiftAnimationManager ()
@property (nonatomic,strong) NSCache *operationCache;/// 操作缓存池
@property (nonatomic, strong) NSOperationQueue *serialQueue;//贵重礼物动画队列
@property (nonatomic, copy) YGPreGiftFinishBlock finishBlock;
@end
@implementation YGPreGiftAnimationManager
- (void)showPreciousGiftAnimationWithGiftCount:(NSInteger)giftCount giftID:(NSInteger)giftID otherParamDict:(NSDictionary *)otherParamDict bgImgView:(UIImageView *)bgImgView animationView:(YGPreciousGiftAnimationView *)animationView webpImgView:(YYAnimatedImageView *)webpImgView finishBlock:(YGPreGiftFinishBlock)finishBlock {
self.finishBlock = finishBlock;
WeakSelf(self);
for (int i = 0; i < giftCount; i ++) {
NSString *animatioKey = [NSString stringWithFormat:@"%ld%@", (long)giftID, [self getMsgId]];
YGPreGiftAnimationOperation *animationOperation = [[YGPreGiftAnimationOperation alloc] initWithAnimationView:animationView giftID:giftID otherParamDict:otherParamDict bgImgView:bgImgView webpImgView:webpImgView finishBlock:^{
[weakSelf.operationCache removeObjectForKey:animatioKey];
if (weakSelf.finishBlock) {
weakSelf.finishBlock();
}
}];
[self.operationCache setObject:animationOperation forKey:animatioKey];
[self.serialQueue addOperation:animationOperation];
}
}
//获取随机msgId
- (NSString *)getMsgId {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSInteger index = (arc4random() % 900) + 100;
return [NSString stringWithFormat:@"%@%@%ld", str, [UserWrapper shareUserWrapper].UID, (long)index];
}
//取消所有操作
- (void)cancelAllAnimationOperation {
[self.serialQueue setSuspended:YES];
[self.serialQueue cancelAllOperations];
}
//贵重礼物动画队列
- (NSOperationQueue *)serialQueue {
if (!_serialQueue) {
_serialQueue = [[NSOperationQueue alloc] init];
_serialQueue.maxConcurrentOperationCount = 1;
}
return _serialQueue;
}
//操作缓存池
- (NSCache *)operationCache {
if (_operationCache==nil) {
_operationCache = [[NSCache alloc] init];
}
return _operationCache;
}
@end
animationOperation :
#import "YGPreGiftAnimationOperation.h"
@interface YGPreGiftAnimationOperation ()
@property (nonatomic, getter = isFinished) BOOL finished;
@property (nonatomic, getter = isExecuting) BOOL executing;
@property (nonatomic, copy) YGPreciousGiftAnimationFinishBlock finishBlock;
@end
@implementation YGPreGiftAnimationOperation
@synthesize finished = _finished;
@synthesize executing = _executing;
- (instancetype)initWithAnimationView:(YGPreciousGiftAnimationView *)view giftID:(NSInteger)giftID otherParamDict:(NSDictionary *)otherParamDict bgImgView:(UIImageView *)bgImgView webpImgView:(YYAnimatedImageView *)webpImgView finishBlock:(YGPreciousGiftAnimationFinishBlock)finishBlock {
self = [super init];
if (self) {
_executing = NO;
_finished = NO;
_view = view;
_giftID = giftID;
_otherParamDict = otherParamDict;
_bgImgView = bgImgView;
_webpImgView = webpImgView;
_finishBlock = finishBlock;
}
return self;
}
- (void)start
{
if ([self isCancelled]) {
self.finished = YES;
return;
}
self.executing = YES;
WeakSelf(self);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
switch (_giftID) {
case 0: //跑车
{
[_view showCarAnimationWithBlock:^{
self.finished = YES;
self.executing = NO;
if (weakSelf.finishBlock) {
weakSelf.finishBlock();
}
}];
}
break;
case 1: //webp
case 2:
case 3:
case 4:
case 5:
{
_webpImgView.hidden = NO;
[_view showWebpAnimationWithGiftID:_giftID otherParamDict:_otherParamDict bgImgView:_bgImgView animationImgView:_webpImgView block:^{
self.finished = YES;
self.executing = NO;
_webpImgView.hidden = YES;
if (weakSelf.finishBlock) {
weakSelf.finishBlock();
}
}];
}
break;
default:
break;
}
}];
}
#pragma mark - 手动触发 KVO
- (void)setExecuting:(BOOL)executing {
[self willChangeValueForKey:@"isExecuting"];
_executing = executing;
[self didChangeValueForKey:@"isExecuting"];
}
- (void)setFinished:(BOOL)finished {
[self willChangeValueForKey:@"isFinished"];
_finished = finished;
[self didChangeValueForKey:@"isFinished"];
}
@end
animationView :
这里主要做动画
我们的项目考虑包的大小, 只有跑车使用了帧动画+UIView的animate动画, 其他大型礼物均使用了webp