iOS 直播间礼物动画队列
主要用到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
iOS 直播间礼物动画队列相关推荐
- ios 直播间点赞动画
直播间点赞动画 #pragma mark - 点赞动画 - (void)praiseAnimation { UIImageView *imageView = [[UIImageView a ...
- Android平台apng动画播放的实现,直播间礼物动画的实现
个人开源工程 ApngDrawable android平台上apng动画播放的实现,基于pngj的java解码库实现,对内存做了很大的优化,目前测试的性能与gif差不多了. 用法 // Init Ap ...
- iOS 直播间送礼物逻辑(礼物连击)
iOS 直播间送礼物 先贴demo地址:GitHub demo基本实现了主流直播间礼物的逻辑 按照队列顺序显示用户所送礼物,累加当前礼物,支持同时显示两人的 礼物 先来最终效果图 一句代码调用送礼物 ...
- 什么是语音聊天软件源码,语音直播间礼物如何开发
语音聊天软件源码是指,能够通过其搭建"用户使用其可以进行语音聊天操作"的软件源码. 语音聊天软件源码的形式是很多样的,它可以以直播的形式出现,可以以多人语音房间的形式出现,也可以用 ...
- Flutter仿抖音点击进入直播间按钮动画实现
利用flutter仿抖音点击进入直播间动画效果 效果图: 对于这个widget 已经封装成插件 供大家依赖使用 askai_animation_button: ^last version 组件的一些必 ...
- iOS直播间聊天室遇到的问题
Question: 1.刷新直播间消息机制该用哪种方法?哪一种更加合适? 2.聊天室该如何图文混排? 3.聊天室出现特殊字符临界点不换行?高度计算错误? 4.聊天室该如何加载网络图片? 5.聊天室如何 ...
- IOS直播平台开发简单的队列效果实现
说到IOS直播平台开发队列的话就想到了多线程,NSOperation ,我们可以重写它,然后在 start 方法中添加动画,但是注意我们只是需要让这些消息排队,更新 UI 还是要在主线程操作:我们还要 ...
- android刷礼物动画demo,GiftSurfaceView 直播间送礼物动画
GiftSurfaceView GiftSurfaceView 最初出自于2014年开发HalloStar项目时所写,主要用于HalloStar项目直播间的送礼物动画.现在想来,那夕阳下的奔跑,是我逝 ...
- 网络直播平台搭建一个直播间的礼物系统
网络直播平台搭建一个直播间的礼物系统 1. 所有直播间的礼物系统,第一步用户看到的无外乎都是礼物的列表界面 纵观主流直播间的礼物列表应该都是使用UICollectionView实现的,所以我也不例外, ...
最新文章
- 【译】IPFS — The Permanent, Distributed Web
- switch两个账号合并_国行Nintendo Switch账号常见问题解答
- asp.net mvc脚手架代码生成工具
- linux 定时任务 crond 服务介绍
- 简单的Python文件服务器和HTTP POST上传文件C代码
- ueditor常见用法
- 【笔记】华为P40手机谷歌play安装与测试笔记
- android模拟器登录qq,手机 上来 个自动 Appium+Python3+夜神安卓模拟器 实现QQ自动登录...
- Linux丢包故障的解决与思路
- 【iOS-Cocos2d游戏开发之十六】添加本地通知(UILocalNotification)以及添加系统组件滚动视图(UIScrollView)!【2011年11月15日更新】
- 翘首期盼的3D仿真练实操全面开启线上学理论,3D练实操,为您插上考证成功的翅膀
- 【U3D入门小白教程——案例篇】之一:球吃豆
- 赚钱的方法分享--首先你要有赚钱的思维和方向计划
- 如何计算CRC校验码(循环冗余检验码)
- h0206. 区间选点
- C语言多线程之“哲学家就餐”问题
- python实用小工具
- 巨量算数 Data解密
- 一文读懂多分类的评价指标(微平均、宏平均、加权平均)
- 树莓派打造成全能路由器