iOS 直播间礼物动画队列

主要用到NSOperation和NSOperationQueue实现动画队列

主要分三个类

animationManager :

#import "YGPreGiftAnimationManager.h"

#import "YGPreGiftAnimationOperation.h"


@interface YGPreGiftAnimationManager ()


@property (nonatomic,strong) NSCache *operationCache;/// 操作缓存池

@property (nonatomic, strong) NSOperationQueue *serialQueue;//贵重礼物动画队列


@property (nonatomic, copyYGPreGiftFinishBlock 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











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值