公屏增加@别人
This commit is contained in:
@@ -42,6 +42,8 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
|
||||
@property (nonatomic,strong) XPRoomMessageHeaderView *headerView;
|
||||
///底部有新的消息
|
||||
@property (nonatomic,strong) UIButton *messageTipsBtn;
|
||||
///有人at你
|
||||
@property (nonatomic, strong) UIButton *atTipBtn;
|
||||
///是否处于正在爬楼
|
||||
@property (nonatomic,assign) BOOL isPending;
|
||||
///是否是最小化进房的
|
||||
@@ -52,6 +54,10 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
|
||||
@property (nonatomic,strong) NSMutableArray<NIMMessage *> *incomingMessages;
|
||||
///气泡数组
|
||||
@property (nonatomic, strong) NSMutableArray *messageBubbles;
|
||||
///有多少人at我
|
||||
@property (nonatomic, assign) NSInteger atCount;
|
||||
///@我的消息位置集合
|
||||
@property (nonatomic, strong) NSMutableArray *locationArray;
|
||||
///messageView 持有这个工具类 进行数据的分发操作 TODO: 有需要在改
|
||||
@property (nonatomic,strong) XPRoomMessageParser *messageParser;
|
||||
///是否是大的 只有在小游戏的时候有用
|
||||
@@ -82,11 +88,143 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
|
||||
[self appendAndScrollToBottom];
|
||||
}
|
||||
|
||||
- (void)atTipsBtnAction:(UIButton *)sender {
|
||||
self.isPending = YES;
|
||||
[self appendAndScrollToAtUser];
|
||||
}
|
||||
|
||||
///追加数据源
|
||||
- (void)appendAndScrollToAtUser {
|
||||
if (self.incomingMessages.count < 1) {
|
||||
NSInteger rows = self.datasource.count;
|
||||
if (!self.locationArray.count) {
|
||||
[self.messageTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(rows-1) inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
|
||||
self.messageTipsBtn.hidden = YES;
|
||||
self.isPending = NO;
|
||||
self.atCount = 0;
|
||||
self.atTipBtn.hidden = YES;
|
||||
return;
|
||||
}
|
||||
int index = [self.locationArray[0] intValue];
|
||||
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
|
||||
if (rows > indexPath.row) {
|
||||
[self.messageTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
|
||||
if (rows == indexPath.row + 1) {
|
||||
self.messageTipsBtn.hidden = YES;
|
||||
self.isPending = NO;
|
||||
}
|
||||
} else {
|
||||
[self.messageTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(rows-1) inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
|
||||
//滚动到底部时隐藏@提示
|
||||
self.messageTipsBtn.hidden = YES;
|
||||
}
|
||||
if (self.locationArray.count) {
|
||||
[self.locationArray removeObjectAtIndex:0];
|
||||
self.atCount -= 1;
|
||||
[self.atTipBtn setTitle:[NSString stringWithFormat:@"有%lu人@你", (unsigned long)self.locationArray.count] forState:UIControlStateNormal];
|
||||
} else {
|
||||
self.atTipBtn.hidden = YES;
|
||||
}
|
||||
} else {
|
||||
if (self.datasource.count > kRoomMessageMaxLength) {
|
||||
NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, kRoomMessageMaxLength/2)];
|
||||
NSArray *needRemoveMsgArray = [self.datasource objectsAtIndexes:set];
|
||||
[self.datasource removeObjectsInArray:needRemoveMsgArray];
|
||||
[self.messageBubbles removeObjectsAtIndexes:set];
|
||||
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
|
||||
for (int i = 0; i<self.locationArray.count; i++) {///移除掉前面消息的数据后,前面如果有at我的消息,也一并移除掉
|
||||
NSNumber *number = self.locationArray[i];
|
||||
if (number.integerValue < kRoomMessageMaxLength / 2) {
|
||||
self.atCount--;
|
||||
[indexSet addIndex:i];
|
||||
}
|
||||
}
|
||||
[self.locationArray removeObjectsAtIndexes:indexSet];
|
||||
for (int i = 0; i<self.locationArray.count; i++) {//移除前面的at消息后,后面的at消息需要更新索引
|
||||
NSNumber *number = self.locationArray[i];
|
||||
number = @(number.integerValue - kRoomMessageMaxLength / 2);
|
||||
[self.locationArray replaceObjectAtIndex:i withObject:number];
|
||||
}
|
||||
}
|
||||
|
||||
// 执行插入
|
||||
NSMutableArray *indexPaths = [NSMutableArray array];
|
||||
for (NIMMessage *item in self.incomingMessages) {
|
||||
[self.datasource addObject:[self.messageParser parseMessageAttribute:item]];
|
||||
[self.messageBubbles addObject:[self.messageParser parseMessageBubble:item]];
|
||||
[indexPaths addObject:[NSIndexPath indexPathForRow:self.datasource.count - 1 inSection:0]];
|
||||
id nickNameNifo = item.remoteExt[@"atUids"];
|
||||
if ([nickNameNifo isKindOfClass:[NSArray class]]) {
|
||||
for (NSString *nick in nickNameNifo) {
|
||||
if ([nick isEqualToString:[AccountInfoStorage instance].getUid]) {
|
||||
[self.locationArray addObject:@(self.datasource.count-1)];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
[self.incomingMessages removeAllObjects];
|
||||
[self.messageTableView reloadData];
|
||||
|
||||
NSInteger rows = self.datasource.count;
|
||||
if (!self.locationArray.count) {
|
||||
[self.messageTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(rows-1) inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
|
||||
self.messageTipsBtn.hidden = YES;
|
||||
self.isPending = NO;
|
||||
self.atCount = 0;
|
||||
self.atTipBtn.hidden = YES;
|
||||
return;
|
||||
}
|
||||
int index = [self.locationArray[0] intValue];
|
||||
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
|
||||
if (rows > indexPath.row) {
|
||||
[self.messageTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
|
||||
if (rows == indexPath.row + 1) {
|
||||
self.messageTipsBtn.hidden = YES;
|
||||
self.isPending = NO;
|
||||
}
|
||||
} else {
|
||||
[self.messageTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(rows-1) inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
|
||||
//滚动到底部时隐藏@提示
|
||||
self.messageTipsBtn.hidden = YES;
|
||||
}
|
||||
if (self.locationArray.count) {
|
||||
[self.locationArray removeObjectAtIndex:0];
|
||||
self.atCount -= 1;
|
||||
[self.atTipBtn setTitle:[NSString stringWithFormat:@"有%lu人@你", (unsigned long)self.locationArray.count] forState:UIControlStateNormal];
|
||||
} else {
|
||||
self.atTipBtn.hidden = YES;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - @我
|
||||
///查找有多少人@我
|
||||
- (void)findAtMeNumber {
|
||||
if (self.incomingMessages.count) {
|
||||
NIMMessage *message = self.incomingMessages.lastObject;
|
||||
id nickNameNifo = message.remoteExt[@"atUids"];
|
||||
if ([nickNameNifo isKindOfClass:[NSArray class]]) {
|
||||
for (NSString *nick in nickNameNifo) {
|
||||
if ([nick isEqualToString:[AccountInfoStorage instance].getUid]) {
|
||||
self.atCount += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (self.atCount > 0) {
|
||||
self.atTipBtn.hidden = NO;
|
||||
[self.atTipBtn setTitle:[NSString stringWithFormat:@"有%zd人@你", self.atCount] forState:UIControlStateNormal];
|
||||
} else {
|
||||
self.atTipBtn.hidden = YES;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Private Method
|
||||
- (void)initSubViews {
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addCustomMessage:) name:@"message" object:nil];
|
||||
[self addSubview:self.messageTableView];
|
||||
[self addSubview:self.messageTipsBtn];
|
||||
[self addSubview:self.atTipBtn];
|
||||
self.messageTableView.tableHeaderView = self.headerView;
|
||||
}
|
||||
|
||||
@@ -103,6 +241,12 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
|
||||
make.bottom.mas_equalTo(self.mas_bottom).offset(-5);
|
||||
make.left.mas_equalTo(self);
|
||||
}];
|
||||
[self.atTipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.width.mas_equalTo(100);
|
||||
make.height.mas_equalTo(30);
|
||||
make.bottom.mas_equalTo(self.mas_bottom).offset(-5);
|
||||
make.left.mas_equalTo(self);
|
||||
}];
|
||||
}
|
||||
|
||||
///是否是当前房间
|
||||
@@ -132,6 +276,7 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
|
||||
}
|
||||
if (self.isPending) {
|
||||
self.messageTipsBtn.hidden = NO;
|
||||
[self findAtMeNumber];
|
||||
} else {
|
||||
[self appendAndScrollToBottom];
|
||||
}
|
||||
@@ -140,6 +285,8 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
|
||||
///追加数据源
|
||||
- (void)appendAndScrollToBottom {
|
||||
if (self.incomingMessages.count < 1) {
|
||||
///滚动到底部(如果有人at自己后,income消息在点击at按钮处做了拼接处理,因为点击at按钮跳转到的是对应的at人消息,如果后面有其他消息时,点有更多按钮时需要滚动到最底部)
|
||||
[self scrollToBottom:YES];
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -172,6 +319,9 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
|
||||
if (r<1) return;
|
||||
NSIndexPath *ip = [NSIndexPath indexPathForRow:r-1 inSection:s-1]; //取最后一行数据
|
||||
[self.messageTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:animated]; //滚动到最后一行
|
||||
self.atCount = 0;
|
||||
self.atTipBtn.hidden = YES;
|
||||
[self.locationArray removeAllObjects];
|
||||
}
|
||||
|
||||
///自定义消息 是否可以加到 公屏 需要自己维护
|
||||
@@ -341,6 +491,9 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
|
||||
[self.datasource removeAllObjects];
|
||||
[self.incomingMessages removeAllObjects];
|
||||
[self.messageBubbles removeAllObjects];
|
||||
[self.locationArray removeAllObjects];
|
||||
self.atCount = 0;
|
||||
self.atTipBtn.hidden = YES;
|
||||
[[XPRoomMiniManager shareManager] resetLocalMessage];
|
||||
[self addRoomMessage:message];
|
||||
return;
|
||||
@@ -474,6 +627,9 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
|
||||
self.isPending = contentSizeH - contentOffsetY - sizeH > 20.0;
|
||||
if (!self.isPending) {
|
||||
self.messageTipsBtn.hidden = YES;
|
||||
self.atTipBtn.hidden = YES;
|
||||
self.atCount = 0;
|
||||
[self.locationArray removeAllObjects];
|
||||
if (self.incomingMessages.count > 0) {
|
||||
[self appendAndScrollToBottom];
|
||||
}
|
||||
@@ -561,6 +717,21 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
|
||||
return _messageTipsBtn;
|
||||
}
|
||||
|
||||
- (UIButton *)atTipBtn {
|
||||
if (!_atTipBtn) {
|
||||
_atTipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
|
||||
[_atTipBtn setTitle:@"有人@你" forState:UIControlStateNormal];
|
||||
[_atTipBtn setTitleColor:[ThemeColor appMainColor] forState:UIControlStateNormal];
|
||||
_atTipBtn.layer.cornerRadius = 15.0;
|
||||
_atTipBtn.layer.masksToBounds = YES;
|
||||
_atTipBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
|
||||
_atTipBtn.backgroundColor = [UIColor whiteColor];
|
||||
[_atTipBtn addTarget:self action:@selector(atTipsBtnAction:) forControlEvents:UIControlEventTouchUpInside];
|
||||
_atTipBtn.hidden = YES;
|
||||
}
|
||||
return _atTipBtn;
|
||||
}
|
||||
|
||||
- (NSMutableArray<NSAttributedString *> *)datasource {
|
||||
if (!_datasource) {
|
||||
_datasource = [NSMutableArray array];
|
||||
@@ -582,6 +753,13 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
|
||||
return _messageBubbles;
|
||||
}
|
||||
|
||||
- (NSMutableArray *)locationArray {
|
||||
if (!_locationArray) {
|
||||
_locationArray = [NSMutableArray array];
|
||||
}
|
||||
return _locationArray;
|
||||
}
|
||||
|
||||
- (XPRoomMessageParser *)messageParser {
|
||||
if (!_messageParser) {
|
||||
_messageParser = [[XPRoomMessageParser alloc] init];
|
||||
|
Reference in New Issue
Block a user