YBLiveChatView.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // YBLiveChatView.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2019/11/30.
  6. // Copyright © 2019 cat. All rights reserved.
  7. //
  8. #import "YBLiveChatView.h"
  9. #import "YBLiveChatModel.h"
  10. #import "YBLiveChatCell.h"
  11. @interface YBLiveChatView()<UITableViewDelegate,UITableViewDataSource>
  12. {
  13. BOOL _canScrollToBottom;
  14. CAGradientLayer *_gradient;
  15. }
  16. @property(nonatomic,strong)UITableView *tableView;
  17. @property(nonatomic,strong)NSMutableArray *chatModels;
  18. @property(nonatomic,strong)NSMutableArray *chatListArray;
  19. @end
  20. @implementation YBLiveChatView
  21. - (instancetype)initWithFrame:(CGRect)frame {
  22. self = [super initWithFrame:frame];
  23. if (self) {
  24. _canScrollToBottom = YES;
  25. _chatListArray = [NSMutableArray array];
  26. _chatModels = [NSMutableArray array];
  27. [self addSubview:self.tableView];
  28. self.layer.shadowColor = UIColor.blackColor.CGColor;
  29. self.layer.shadowOffset = CGSizeMake(0, -5);
  30. self.layer.shadowOpacity = 0.6;
  31. self.layer.shadowRadius = 5;
  32. self.clipsToBounds = NO;
  33. }
  34. return self;
  35. }
  36. -(NSArray *)chatModels{
  37. NSMutableArray *array = [NSMutableArray array];
  38. for (NSDictionary *dic in _chatListArray) {
  39. YBLiveChatModel *model = [YBLiveChatModel modelWithDic:dic];
  40. [array addObject:model];
  41. }
  42. _chatModels = [array mutableCopy];
  43. return _chatModels;
  44. }
  45. //刷新消息
  46. -(void)reloadMsg:(NSDictionary *)dic {
  47. [_chatListArray addObject:dic];
  48. if (_chatListArray.count>30) {
  49. [_chatListArray removeObjectAtIndex:0];
  50. }
  51. [self.tableView reloadData];
  52. if (_canScrollToBottom) {
  53. NSUInteger sectionCount = [_tableView numberOfSections];
  54. if (sectionCount) {
  55. NSUInteger rowCount = [_tableView numberOfRowsInSection:0];
  56. if (rowCount) {
  57. NSUInteger ii[2] = {sectionCount-1, 0};
  58. NSIndexPath* indexPath = [NSIndexPath indexPathWithIndexes:ii length:2];
  59. [_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  60. }
  61. }
  62. }
  63. }
  64. /** 切换房间清理上个房间的聊天记录 */
  65. -(void)clearChatList {
  66. [_chatListArray removeAllObjects];
  67. [self.tableView reloadData];
  68. }
  69. #pragma mark - UITableViewDelegate、UITableViewDataSource
  70. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  71. return 5;
  72. }
  73. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  74. UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, 5)];
  75. view.backgroundColor = [UIColor clearColor];
  76. return view;
  77. }
  78. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  79. return UITableViewAutomaticDimension;
  80. }
  81. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  82. return self.chatModels.count;
  83. }
  84. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  85. return 1;
  86. }
  87. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  88. YBLiveChatCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YBLiveChatCell"];
  89. if (!cell) {
  90. cell = [[[NSBundle mainBundle]loadNibNamed:@"YBLiveChatCell" owner:nil options:nil]objectAtIndex:0];
  91. }
  92. cell.model = _chatModels[indexPath.section];
  93. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  94. return cell;
  95. }
  96. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  97. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  98. YBLiveChatModel *model = _chatModels[indexPath.section];
  99. if ([model.userName isEqual:YZMsg(@"直播间消息")]) {
  100. return;
  101. }
  102. if (self.liveChatEvent) {
  103. self.liveChatEvent(@"房间聊天-用户信息", @{@"id":model.userID,@"name":model.userName});
  104. }
  105. }
  106. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
  107. _canScrollToBottom = NO;
  108. }
  109. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  110. _canScrollToBottom = YES;
  111. }
  112. #pragma mark - set/get
  113. -(UITableView *)tableView {
  114. if (!_tableView) {
  115. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, self.width, self.height)style:UITableViewStylePlain];
  116. _tableView.delegate = self;
  117. _tableView.dataSource = self;
  118. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  119. _tableView.backgroundColor = UIColor.clearColor;
  120. _tableView.estimatedRowHeight = 80.0;
  121. _tableView.estimatedSectionFooterHeight = 0;
  122. _tableView.estimatedSectionHeaderHeight = 0;
  123. _tableView.showsVerticalScrollIndicator = NO;
  124. }
  125. return _tableView;
  126. }
  127. @end