TCShowVoiceBottomView.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. //
  2. // TCShowLiveBottomView.m
  3. // TCShow
  4. //
  5. // Created by AlexiChen on 16/4/14.
  6. // Copyright © 2016年 AlexiChen. All rights reserved.
  7. //
  8. #import "TCShowVoiceBottomView.h"
  9. #import "UIView+CustomAutoLayout.h"
  10. @implementation TCShowVoiceBottomView
  11. #pragma mark - ----------------------- 直播生命周期 -----------------------
  12. - (void)releaseAll
  13. {
  14. [[NSNotificationCenter defaultCenter] removeObserver:self];
  15. [self.bjsbadge removeFromSuperview];
  16. [self.jsbadge removeFromSuperview];
  17. }
  18. - (void)dealloc
  19. {
  20. [self releaseAll];
  21. }
  22. #pragma mark 开始直播
  23. - (void)startLive
  24. {
  25. }
  26. #pragma mark 暂停直播
  27. - (void)pauseLive
  28. {
  29. }
  30. #pragma mark 重新开始直播
  31. - (void)resumeLive
  32. {
  33. }
  34. #pragma mark 结束直播
  35. - (void)endLive
  36. {
  37. [self releaseAll];
  38. }
  39. #pragma mark - ----------------------- 其他 -----------------------
  40. - (instancetype)initWithFrame:(CGRect)frame
  41. {
  42. if (self = [super initWithFrame:frame])
  43. {
  44. #if kSupportIMMsgCache
  45. // 创建点赞消息缓存
  46. self.praiseImageCache = [NSMutableArray array];
  47. // 预先缓存点赞动画图片,主要是防止在收到大量点赞消息时,因加载资源的耗时
  48. /**
  49. UIImage *img = [UIImage imageNamed:@"img_like"];
  50. [self.praiseImageCache addObject:[img imageWithTintColor:[UIColor flatRedColor]]];
  51. [self.praiseImageCache addObject:[img imageWithTintColor:[UIColor flatDarkRedColor]]];
  52. */
  53. [self.praiseImageCache addObject:[UIImage imageNamed:@"heart0"]];
  54. [self.praiseImageCache addObject:[UIImage imageNamed:@"heart1"]];
  55. [self.praiseImageCache addObject:[UIImage imageNamed:@"heart2"]];
  56. [self.praiseImageCache addObject:[UIImage imageNamed:@"heart3"]];
  57. [self.praiseImageCache addObject:[UIImage imageNamed:@"heart4"]];
  58. [self.praiseImageCache addObject:[UIImage imageNamed:@"heart5"]];
  59. [self.praiseImageCache addObject:[UIImage imageNamed:@"heart6"]];
  60. self.praiseAnimationCache = [NSMutableArray array];
  61. #endif
  62. _canSendLightMsg = YES;
  63. _showFuncs = [NSMutableArray array];
  64. _menusBottomView = [[UIView alloc]init];
  65. _menusBottomView.backgroundColor = kClearColor;
  66. [self addSubview:_menusBottomView];
  67. //发送消息按钮
  68. FWWeakify(self)
  69. // _msgMenuBtn = [[MenuButton alloc] initWithTitle:nil icon:[UIImage imageNamed:@"lr_bottom_comment"] action:^(id<MenuAbleItem> menu) {
  70. // FWStrongify(self)
  71. // [self menuButtonAction:menu];
  72. // }];
  73. _msgMenuBtn = [[MenuButton alloc] initWithTitle:nil icon:[UIImage imageNamed:@"lr_bottom_comment"] action:^(id<MenuAbleItem> menu){
  74. NSLog(@"tag _ %d",menu.tag);
  75. FWStrongify(self)
  76. [self menuButtonAction:menu];
  77. }];
  78. [_msgMenuBtn setImage:nil forState:UIControlStateNormal];
  79. [_msgMenuBtn setBackgroundImage:[UIImage imageNamed:@"roomchatbtnbg"] forState:UIControlStateNormal];
  80. _msgMenuBtn.backgroundColor = [UIColor colorWithHex:0x000000 andAlpha:0.2];
  81. [_msgMenuBtn setTitle:ASLocalizedString(@"一起聊聊") forState:UIControlStateNormal];
  82. _msgMenuBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  83. [_msgMenuBtn setTitleColor:[UIColor colorWithHex:0xFFFFFF andAlpha:0.6] forState:UIControlStateNormal];
  84. _msgMenuBtn.tag = EFunc_VOICE_INPUT;
  85. // [_msgMenuBtn setImage:[UIImage imageNamed:@"lr_bottom_comment"] forState:UIControlStateHighlighted];
  86. /**
  87. // 购物
  88. if ([self.BuguLive.appModel.shopping_goods integerValue] == 1 || self.BuguLive.appModel.open_podcast_goods == 1) {
  89. _starShopBtn = [[MenuButton alloc] initWithTitle:nil icon:[UIImage imageNamed:@"fw_liveRoom_tools"] action:^(id<MenuAbleItem> menu) {
  90. [ws menuButtonAction:menu];
  91. }];
  92. _starShopBtn.tag = EFunc_STARSHOP ;
  93. _starShopBtn.hidden = YES;
  94. [_starShopBtn setImage:[UIImage imageNamed:@"fw_liveRoom_tools"] forState:UIControlStateHighlighted];
  95. }
  96. */
  97. // 更多功能
  98. if ([self.BuguLive.appModel.shopping_goods integerValue] == 1 || self.BuguLive.appModel.open_podcast_goods == 1)
  99. {
  100. _moreToolsBtn = [[MenuButton alloc] initWithTitle:nil icon:[UIImage imageNamed:@"lr_bottom_tools"] action:^(id<MenuAbleItem> menu) {
  101. FWStrongify(self)
  102. [self menuButtonAction:menu];
  103. }];
  104. _moreToolsBtn.tag = EFunc_MORETOOLS;
  105. _moreToolsBtn.hidden = YES;
  106. [_moreToolsBtn setImage:[UIImage imageNamed:@"lr_bottom_tools"] forState:UIControlStateHighlighted];
  107. }
  108. // 插件中心
  109. _gamesBtn = [[MenuButton alloc] initWithTitle:nil icon:[UIImage imageNamed:@"lr_bottom_tools"] action:^(id<MenuAbleItem> menu) {
  110. FWStrongify(self)
  111. [self menuButtonAction:menu];
  112. }];
  113. _gamesBtn.tag = EFunc_GAMES ;
  114. [_gamesBtn setImage:[UIImage imageNamed:@"lr_bottom_tools"] forState:UIControlStateHighlighted];
  115. _gamesBtn.hidden = YES;
  116. // 开始游戏
  117. _beginGameBtn = [[MenuButton alloc] initWithTitle:nil icon:[UIImage imageNamed:@"gm_game_licensing"] action:^(id<MenuAbleItem> menu) {
  118. FWStrongify(self)
  119. [self menuButtonAction:menu];
  120. }];
  121. _beginGameBtn.tag = EFunc_BEGINGAMES;
  122. _beginGameBtn.hidden = YES;
  123. [_beginGameBtn setImage:[UIImage imageNamed:@"gm_game_licensing"] forState:UIControlStateHighlighted];
  124. //游戏视图显示与隐藏开关
  125. _switchGameViewBtn = [[MenuButton alloc] initWithTitle:nil icon:[UIImage imageNamed:@"gm_game_closeView"] action:^(id<MenuAbleItem> menu) {
  126. FWStrongify(self)
  127. [self menuButtonAction:menu];
  128. }];
  129. _switchGameViewBtn.tag = EFunc_SWITCHGAMEVIEW;
  130. _switchGameViewBtn.hidden = YES;
  131. [_switchGameViewBtn setImage:[UIImage imageNamed:@"gm_game_closeView"] forState:UIControlStateHighlighted];
  132. if(self.BuguLive.appModel.open_banker_module == 1)
  133. {
  134. _switchBankerBtn = [[MenuButton alloc] initWithTitle:nil icon:[UIImage imageNamed:@"gm_open_banker"] action:^(id<MenuAbleItem> menu) {
  135. FWStrongify(self)
  136. [self menuButtonAction:menu];
  137. }];
  138. _switchBankerBtn.tag = EFunc_SWITCH_BANNER;
  139. _switchBankerBtn.hidden = YES;
  140. [_switchBankerBtn setImage:[UIImage imageNamed:@"gm_open_banker"] forState:UIControlStateHighlighted];
  141. _grabBankerBtn = [[MenuButton alloc] initWithTitle:nil icon:[UIImage imageNamed:@"gm_grab_banker"] action:^(id<MenuAbleItem> menu) {
  142. FWStrongify(self)
  143. [self menuButtonAction:menu];
  144. }];
  145. _grabBankerBtn.tag = EFunc_GRAB_BANNER;
  146. _grabBankerBtn.hidden = YES;
  147. [_grabBankerBtn setImage:[UIImage imageNamed:@"gm_grab_banker"] forState:UIControlStateHighlighted];
  148. }
  149. //这个后期 统一处理 不写成通知
  150. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(newMsgNotification:) name:@"reloadunread" object:nil];
  151. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(newMsgNotification:) name:g_notif_chatmsg object:nil];
  152. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(newMsgNotification:) name:@"imRemoveNeedUpdate" object:nil];
  153. }
  154. return self;
  155. }
  156. - (void)refreshLiveItem:(id<FWShowLiveRoomAble>)liveItem liveInfo:(CurrentLiveInfo *)liveInfo
  157. {
  158. _liveInfo = liveInfo;
  159. [self addLiveFunc:[liveItem liveType]];
  160. }
  161. #pragma mark 主播需要添加的按钮
  162. - (void)hostAddFunc
  163. {
  164. [self addFunc:EFunc_VOICE_FACE];
  165. [self addFunc:EFunc_VOICE_MUSIC];
  166. [self addFunc:EFunc_VOICE_CONNECT_MIKE];
  167. [self addFunc:EFunc_VOICE_MORE];
  168. [self addFunc:EFunc_VOICE_CHART];
  169. [self addFunc:EFunc_VOICE_GIFT];
  170. [self addFunc:EFunc_VOICE_GAME];
  171. }
  172. #pragma mark 观众需要添加的按钮
  173. - (void)audienceAddFunc
  174. {
  175. [self addFunc:EFunc_VOICE_FACE];
  176. [self addFunc:EFunc_VOICE_MUSIC];
  177. [self addFunc:EFunc_VOICE_CHART];
  178. [self addFunc:EFunc_VOICE_GIFT];
  179. [self addFunc:EFunc_VOICE_GAME];
  180. }
  181. #pragma mark 添加菜单
  182. - (void)addLiveFunc:(NSInteger)liveType
  183. {
  184. if (_menusBottomView)
  185. {
  186. [_menusBottomView removeFromSuperview];
  187. _menusBottomView = nil;
  188. _menusBottomView = [[UIView alloc]init];
  189. _menusBottomView.backgroundColor = kClearColor;
  190. [self addSubview:_menusBottomView];
  191. [_showFuncs removeAllObjects];
  192. }
  193. if (liveType == FW_LIVE_TYPE_HOST)
  194. {
  195. [_moreToolsBtn removeFromSuperview];
  196. _moreToolsBtn = nil;
  197. _gamesBtn.hidden = YES;
  198. [self hostAddFunc];
  199. }
  200. else if (liveType == FW_LIVE_TYPE_AUDIENCE)
  201. {
  202. _moreToolsBtn.hidden = NO;
  203. _gamesBtn.hidden = YES;
  204. [self audienceAddFunc];
  205. }
  206. [self relayoutFrameOfSubViews];
  207. }
  208. - (void)addFunc:(NSInteger)funcid
  209. {
  210. MenuButton *menuBtn = nil;
  211. FWWeakify(self)
  212. switch (funcid)
  213. {
  214. case EFunc_VOICE_MUSIC:
  215. {
  216. menuBtn = [[MenuButton alloc] initWithTitle:nil icon:[UIImage imageNamed:@"lr_bottom_music"] action:^(id<MenuAbleItem> menu) {
  217. FWStrongify(self)
  218. [self menuButtonAction:menu];
  219. }];
  220. [menuBtn setImage:[UIImage imageNamed:@"lr_bottom_gift_list"] forState:UIControlStateSelected];
  221. }
  222. break;
  223. case EFunc_VOICE_FACE:
  224. {
  225. menuBtn = [[MenuButton alloc] initWithTitle:nil icon:[UIImage imageNamed:@"lr_bottom_full_face"] action:^(id<MenuAbleItem> menu) {
  226. FWStrongify(self)
  227. [self menuButtonAction:menu];
  228. }];
  229. [menuBtn setImage:[UIImage imageNamed:@"lr_bottom_gift_list"] forState:UIControlStateSelected];
  230. }
  231. break;
  232. case EFunc_VOICE_GIFT:
  233. {
  234. menuBtn = [[MenuButton alloc] initWithTitle:nil icon:[UIImage imageNamed:@"语音房间底部_礼物"] action:^(id<MenuAbleItem> menu) {
  235. FWStrongify(self)
  236. [self menuButtonAction:menu];
  237. }];
  238. [menuBtn setImage:[UIImage imageNamed:@"lr_bottom_gift_list"] forState:UIControlStateSelected];
  239. }
  240. break;
  241. case EFunc_VOICE_GAME:
  242. {
  243. menuBtn = [[MenuButton alloc] initWithTitle:nil icon:[UIImage imageNamed:@"更多 (4)"] action:^(id<MenuAbleItem> menu) {
  244. FWStrongify(self)
  245. [self menuButtonAction:menu];
  246. }];
  247. [menuBtn setImage:[UIImage imageNamed:@"lr_bottom_gift_list"] forState:UIControlStateSelected];
  248. }
  249. break;
  250. case EFunc_VOICE_CONNECT_MIKE:
  251. {
  252. menuBtn = [[MenuButton alloc] initWithTitle:nil icon:[UIImage imageNamed:@"语音房间底部_麦位管理"] action:^(id<MenuAbleItem> menu) {
  253. FWStrongify(self)
  254. [self menuButtonAction:menu];
  255. }];
  256. [menuBtn setImage:[UIImage imageNamed:@"lr_bottom_gift_list"] forState:UIControlStateSelected];
  257. }
  258. break;
  259. case EFunc_VOICE_CHART:
  260. {
  261. menuBtn = [[MenuButton alloc] initWithTitle:nil icon:[UIImage imageNamed:@"语音房间底部_消息"] action:^(id<MenuAbleItem> menu) {
  262. FWStrongify(self)
  263. [self menuButtonAction:menu];
  264. }];
  265. [menuBtn setImage:[UIImage imageNamed:@"lr_bottom_gift_list"] forState:UIControlStateSelected];
  266. }
  267. break;
  268. case EFunc_VOICE_MORE:
  269. {
  270. menuBtn = [[MenuButton alloc] initWithTitle:nil icon:[UIImage imageNamed:@"more3"] action:^(id<MenuAbleItem> menu) {
  271. FWStrongify(self)
  272. [self menuButtonAction:menu];
  273. }];
  274. [menuBtn setImage:[UIImage imageNamed:@"more3"] forState:UIControlStateSelected];
  275. }
  276. break;
  277. default:
  278. break;
  279. }
  280. if (menuBtn)
  281. {
  282. menuBtn.tag = funcid;
  283. [_menusBottomView addSubview:menuBtn];
  284. [_showFuncs addObject:menuBtn];
  285. }
  286. }
  287. - (void)addMsgBadge:(MenuButton *)menuBtn
  288. {
  289. FWWeakify(self)
  290. [SFriendObj getAllUnReadCountComplete:^(int num) {
  291. FWStrongify(self)
  292. int jj = num;
  293. self.jsbadge = [[JSBadgeView alloc]initWithParentView:menuBtn alignment:JSBadgeViewAlignmentTopRight];
  294. if(jj)
  295. {
  296. if(jj > 98)
  297. {
  298. self.jsbadge.badgeText = @"99+";
  299. }
  300. else
  301. {
  302. self.jsbadge.badgeText = [NSString stringWithFormat:@"%d",jj];
  303. }
  304. }
  305. else
  306. {
  307. self.jsbadge.badgeText = nil;
  308. }
  309. FWWeakify(self)
  310. [self xw_addNotificationForName:@"clearJsbadge" block:^(NSNotification * _Nonnull notification) {
  311. FWStrongify(self)
  312. self.jsbadge.badgeText = nil;
  313. }];
  314. }];
  315. }
  316. - (void)newMsgNotification:(NSNotification*)notifiation
  317. {
  318. [SFriendObj getAllUnReadCountComplete:^(int num) {
  319. if(num)
  320. {
  321. if(num >98)
  322. {
  323. self.jsbadge.badgeText = @"99+";
  324. }
  325. else
  326. {
  327. self.jsbadge.badgeText = [NSString stringWithFormat:@"%d",num];
  328. }
  329. }
  330. else
  331. {
  332. self.jsbadge.badgeText = nil;
  333. }
  334. }];
  335. }
  336. #pragma mark 设置视图frame
  337. - (void)relayoutFrameOfSubViews
  338. {
  339. CGFloat multiple;
  340. if (kScreenW <375 && _showFuncs.count > 4)
  341. {
  342. multiple = 0.3;//间距要缩小要不然 320 显示不下
  343. }
  344. else
  345. {
  346. multiple = 1.0;
  347. }
  348. CGFloat tmpWidth = _showFuncs.count * self.bounds.size.height + multiple*kDefaultMargin * (_showFuncs.count-1);
  349. _menusBottomView.frame = CGRectMake(CGRectGetWidth(self.frame)-tmpWidth-kDefaultMargin, 0, tmpWidth, self.bounds.size.height);
  350. CGRect rect = _menusBottomView.bounds;
  351. rect = CGRectInset(rect, 0, 0);
  352. if (_showFuncs.count)
  353. {
  354. [_menusBottomView alignSubviews:_showFuncs horizontallyWithPadding:kDefaultMargin*multiple margin:0 inRect:rect];
  355. }
  356. //发送消息按钮
  357. _msgMenuBtn.frame = CGRectMake(kDefaultMargin, 0, 80, self.bounds.size.height);
  358. ViewRadius(_msgMenuBtn, self.bounds.size.height/2);
  359. [self addSubview:_msgMenuBtn];
  360. if ([self.BuguLive.appModel.shopping_goods integerValue] == 1 || self.BuguLive.appModel.open_podcast_goods == 1 )
  361. {
  362. //添加观众端更多工具按钮
  363. [self addSubview:_moreToolsBtn];
  364. _moreToolsBtn.frame = CGRectMake(CGRectGetMaxX(_msgMenuBtn.frame)+kDefaultMargin, 0, self.bounds.size.height, self.bounds.size.height);
  365. }
  366. #if kSupportH5Shopping
  367. _starShopBtn.frame = CGRectMake(2*kDefaultMargin+self.bounds.size.height, 0, 2*self.bounds.size.height, self.bounds.size.height);
  368. [self addSubview:_starShopBtn];
  369. #endif
  370. if (_isHost)
  371. {
  372. // _gamesBtn.frame = CGRectMake(CGRectGetMaxX(_msgMenuBtn.frame)+kDefaultMargin, 0, self.bounds.size.height, self.bounds.size.height);
  373. // [self addSubview:_gamesBtn];
  374. _beginGameBtn.frame = CGRectMake(CGRectGetMaxX(_gamesBtn.frame)+kDefaultMargin, 0, self.bounds.size.height, self.bounds.size.height);
  375. [self addSubview:_beginGameBtn];
  376. _switchGameViewBtn.frame = CGRectMake(CGRectGetMaxX(_beginGameBtn.frame)+kDefaultMargin, 0, self.bounds.size.height, self.bounds.size.height);
  377. [self addSubview:_switchGameViewBtn];
  378. _switchBankerBtn.frame = CGRectMake(CGRectGetMaxX(_switchGameViewBtn.frame)+kDefaultMargin, 30, self.bounds.size.height, self.bounds.size.height);
  379. [self addSubview:_switchBankerBtn];
  380. if(!self.bjsbadge)
  381. {
  382. self.bjsbadge = [[JSBadgeView alloc]initWithParentView:_switchBankerBtn alignment:JSBadgeViewAlignmentTopRight];
  383. }
  384. if(_bankMessage)
  385. {
  386. self.bjsbadge.badgeText = @"···";
  387. }
  388. else
  389. {
  390. self.bjsbadge.badgeText = nil;
  391. }
  392. }
  393. else
  394. {
  395. if ([self.BuguLive.appModel.shopping_goods integerValue] == 1 || self.BuguLive.appModel.open_podcast_goods == 1)
  396. {
  397. _switchGameViewBtn.frame = CGRectMake(CGRectGetMaxX(_moreToolsBtn.frame)+kDefaultMargin, 0, self.bounds.size.height, self.bounds.size.height);
  398. }
  399. else
  400. {
  401. _switchGameViewBtn.frame = CGRectMake(CGRectGetMaxX(_msgMenuBtn.frame)+kDefaultMargin, 0, self.bounds.size.height, self.bounds.size.height);
  402. }
  403. [self addSubview:_switchGameViewBtn];
  404. _grabBankerBtn.frame = CGRectMake(CGRectGetMaxX(_switchGameViewBtn.frame)+kDefaultMargin, 0, self.bounds.size.height, self.bounds.size.height);
  405. [self addSubview:_grabBankerBtn];
  406. }
  407. }
  408. #pragma mark 按钮点击事件
  409. - (void)menuButtonAction:(MenuButton *)btn
  410. {
  411. if (_delegate && [_delegate respondsToSelector:@selector(onBottomViewClickMenus:fromButton:)])
  412. {
  413. [_delegate onBottomViewClickMenus:self fromButton:btn];
  414. }
  415. }
  416. - (void)onEnableInteractCamera:(MenuButton *)btn
  417. {
  418. if ([_multiDelegate respondsToSelector:@selector(onBottomView:operateCameraOf:fromButton:)])
  419. {
  420. [_multiDelegate onBottomView:self operateCameraOf:_showUser fromButton:btn];
  421. }
  422. }
  423. - (void)onEnableInteractMic:(MenuButton *)btn
  424. {
  425. if ([_multiDelegate respondsToSelector:@selector(onBottomView:operateMicOf:fromButton:)])
  426. {
  427. [_multiDelegate onBottomView:self operateMicOf:_showUser fromButton:btn];
  428. }
  429. }
  430. - (void)onCancelInteract:(MenuButton *)btn
  431. {
  432. if ([_multiDelegate respondsToSelector:@selector(onBottomView:cancelInteractWith:fromButton:)])
  433. {
  434. [_multiDelegate onBottomView:self cancelInteractWith:_showUser fromButton:btn];
  435. }
  436. }
  437. - (void)onSwtichSpeaker:(MenuButton *)btn
  438. {
  439. btn.selected = !btn.selected;
  440. btn.enabled = NO;
  441. }
  442. - (void)showLight
  443. {
  444. if (_canSendLightMsg)
  445. {
  446. _canSendLightMsg = NO;
  447. FWWeakify(self)
  448. // 点赞消息产生的动画,大量产生时非常耗性能,建议观众端不要频繁发送
  449. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  450. FWStrongify(self)
  451. self.canSendLightMsg = YES;
  452. });
  453. }
  454. }
  455. - (void)showLikeHeart
  456. {
  457. // 非纯净模式下显示点赞消息
  458. [self showLikeHeartStartRect:_heartRect];
  459. }
  460. #if kSupportIMMsgCache
  461. - (void)showLikeHeart:(AVIMCache *)cache
  462. {
  463. if (cache.count)
  464. {
  465. // 非纯净模式下显示点赞消息
  466. [self showLikeHeartStartRect:_heartRect count:cache.count > 5 ? 5 : cache.count];
  467. }
  468. else
  469. {
  470. // 没有的时候,释放缓存
  471. if (self.praiseAnimationCache.count)
  472. {
  473. UIImageView *imageView = [self.praiseAnimationCache objectAtIndex:0];
  474. [self.praiseAnimationCache removeObject:imageView];
  475. [imageView removeFromSuperview];
  476. }
  477. }
  478. }
  479. #endif
  480. - (CAAnimation *)hearAnimationFrom:(CGRect)frame
  481. {
  482. //位置
  483. CAKeyframeAnimation *animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
  484. animation.beginTime = 0.5;
  485. animation.duration = 2.5;
  486. animation.removedOnCompletion = YES;
  487. animation.fillMode = kCAFillModeForwards;
  488. animation.repeatCount= 0;
  489. animation.calculationMode = kCAAnimationCubicPaced;
  490. CGMutablePathRef curvedPath = CGPathCreateMutable();
  491. CGPoint point0 = CGPointMake(frame.origin.x + frame.size.width / 2, frame.origin.y + frame.size.height / 2);
  492. CGPathMoveToPoint(curvedPath, NULL, point0.x, point0.y);
  493. float x11 = point0.x - arc4random() % 30 + 30;
  494. float y11 = frame.origin.y - arc4random() % 60 ;
  495. float x1 = point0.x - arc4random() % 15 + 15;
  496. float y1 = frame.origin.y - arc4random() % 60 - 30;
  497. CGPoint point1 = CGPointMake(x1, y1);
  498. CGPathAddQuadCurveToPoint(curvedPath, NULL, x11, y11, point1.x, point1.y);
  499. int conffset2 = self.superview.bounds.size.width * 0.2;
  500. int conffset21 = self.superview.bounds.size.width * 0.1;
  501. float x2 = point0.x - arc4random() % conffset2 + conffset2;
  502. float y2 = arc4random() % 30 + 240;
  503. float x21 = point0.x - arc4random() % conffset21 + conffset21;
  504. float y21 = (y2 + y1) / 2 + arc4random() % 30 - 30;
  505. CGPoint point2 = CGPointMake(x2, y2);
  506. CGPathAddQuadCurveToPoint(curvedPath, NULL, x21, y21, point2.x, point2.y);
  507. animation.path = curvedPath;
  508. CGPathRelease(curvedPath);
  509. //透明度变化
  510. CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
  511. opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];
  512. opacityAnim.toValue = [NSNumber numberWithFloat:0];
  513. opacityAnim.removedOnCompletion = NO;
  514. opacityAnim.beginTime = 0;
  515. opacityAnim.duration = 3;
  516. //比例
  517. CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  518. // int scale = arc4random() % 5 + 5;
  519. scaleAnim.fromValue = [NSNumber numberWithFloat:.0];//[NSNumber numberWithFloat:((float)scale / 10)];
  520. scaleAnim.toValue = [NSNumber numberWithFloat:1];
  521. scaleAnim.removedOnCompletion = NO;
  522. scaleAnim.fillMode = kCAFillModeForwards;
  523. scaleAnim.duration = .5;
  524. CAAnimationGroup *animGroup = [CAAnimationGroup animation];
  525. animGroup.animations = [NSArray arrayWithObjects: scaleAnim,opacityAnim,animation, nil];
  526. animGroup.duration = 3;
  527. return animGroup;
  528. }
  529. - (void)showLikeHeartStartRect:(CGRect)frame
  530. {
  531. _lightCount ++;
  532. UIImageView *imageView = [[UIImageView alloc ] initWithFrame:frame];
  533. // imageView.image = [[UIImage imageNamed:@"img_like"] imageWithTintColor:[UIColor randomFlatDarkColor]];
  534. if (_heartImgViewName)
  535. {
  536. imageView.image = [UIImage imageNamed:_heartImgViewName];
  537. }
  538. else
  539. {
  540. int index = arc4random() % 6;
  541. imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"heart%d",index]];
  542. }
  543. [self.superview addSubview:imageView];
  544. imageView.alpha = 0;
  545. [imageView.layer addAnimation:[self hearAnimationFrom:frame] forKey:nil];
  546. FWWeakify(self)
  547. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  548. FWStrongify(self)
  549. [imageView removeFromSuperview];
  550. self.lightCount --;
  551. });
  552. }
  553. #if kSupportIMMsgCache
  554. - (void)showLikeHeartStartRect:(CGRect)frame count:(NSInteger)count
  555. {
  556. for (int i = 0; i < count; i++)
  557. {
  558. [self showLikeHeartStartRectFrameCache:frame];
  559. }
  560. }
  561. - (void)showLikeHeartStartRectFrameCache:(CGRect)frame
  562. {
  563. UIImageView *imageView = nil;
  564. if (self.praiseAnimationCache.count)
  565. {
  566. imageView = [self.praiseAnimationCache objectAtIndex:0];
  567. [self.praiseAnimationCache removeObject:imageView];
  568. imageView.frame = frame;
  569. imageView.hidden = NO;
  570. }
  571. else
  572. {
  573. imageView = [[UIImageView alloc ] initWithFrame:frame];
  574. imageView.image = self.praiseImageCache[arc4random()%self.praiseImageCache.count];
  575. [self.superview addSubview:imageView];
  576. }
  577. imageView.alpha = 0;
  578. [imageView.layer addAnimation:[self hearAnimationFrom:frame] forKey:nil];
  579. FWWeakify(self)
  580. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  581. FWStrongify(self)
  582. [self.praiseAnimationCache addObject:imageView];
  583. });
  584. }
  585. #endif
  586. - (void)updateShowFunc
  587. {
  588. }
  589. @end