优化礼物消息的动画效果,减少视觉冲突。修改消息插入动画为淡入效果,延迟滚动执行,确保动画流畅性。同时新增礼物消息识别方法,优化 Cell 更新逻辑以避免动画期间的布局更新。

This commit is contained in:
edwinQQQ
2025-08-28 15:16:26 +08:00
parent d22ddaefcf
commit d4ac93adbb
3 changed files with 193 additions and 32 deletions

View File

@@ -29,6 +29,8 @@
@property (nonatomic,strong) UITapGestureRecognizer *tapEmptyRecognizer;
@property (nonatomic, strong) UIView *bottomSpace;
///
@property (nonatomic, assign) BOOL isAnimating;
@end
@@ -153,26 +155,42 @@
#pragma mark - Getters And Setters
- (void)setMessageInfo:(XPMessageInfoModel *)messageInfo {
//
//
if (_messageInfo &&
[messageInfo.content isEqualToAttributedString:_messageInfo.content] &&
[messageInfo.bubbleImageUrl isEqualToString:_messageInfo.bubbleImageUrl] &&
[messageInfo.boomImageUrl isEqualToString:_messageInfo.boomImageUrl]) {
[messageInfo.boomImageUrl isEqualToString:_messageInfo.boomImageUrl] &&
messageInfo.first == _messageInfo.first &&
messageInfo.second == _messageInfo.second) {
return;
}
_messageInfo = messageInfo;
if (messageInfo) {
// attributedTexthasBubble
BOOL hasBubble = ![NSString isEmpty:messageInfo.bubbleImageUrl];
if (self.contentLabel.hasBubble != hasBubble) {
self.contentLabel.hasBubble = hasBubble;
//
if (self.isAnimating) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self setMessageInfo:messageInfo];
});
return;
}
self.contentLabel.attributedText = messageInfo.content;
// 使 performWithoutAnimation
[UIView performWithoutAnimation:^{
// attributedTexthasBubble
BOOL hasBubble = ![NSString isEmpty:messageInfo.bubbleImageUrl];
if (self.contentLabel.hasBubble != hasBubble) {
self.contentLabel.hasBubble = hasBubble;
}
self.contentLabel.attributedText = messageInfo.content;
}];
//
if (self.isLeftBigImage && messageInfo.boomImageUrl) {
self.leftBigImageView.imageUrl = messageInfo.boomImageUrl;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.leftBigImageView.imageUrl = messageInfo.boomImageUrl;
});
}
}
}
@@ -190,9 +208,13 @@
return;
}
[self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(size.width);
}];
// 使
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(size.width);
}];
[self layoutIfNeeded];
} completion:nil];
// [self.bubbleImageView mas_updateConstraints:^(MASConstraintMaker *make) {
// if (size.width + 32 >= kRoomMessageMaxWidth) {
@@ -206,13 +228,17 @@
if (hasBubble) {
@kWeakify(self);
[self.bubbleImageView loadImageWithUrl:self.messageInfo.bubbleImageUrl
completion:^(UIImage * _Nonnull image, NSURL * _Nonnull url) {
@kStrongify(self);
UIImage *image1 = [UIImage imageWithCGImage:image.CGImage scale:2.0 orientation:UIImageOrientationUp];
UIImage *cutImage = [image1 cropRightAndBottomPixels:2];
self.bubbleImageView.image = [self resizableImage:cutImage];
}];
//
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.bubbleImageView loadImageWithUrl:self.messageInfo.bubbleImageUrl
completion:^(UIImage * _Nonnull image, NSURL * _Nonnull url) {
@kStrongify(self);
UIImage *image1 = [UIImage imageWithCGImage:image.CGImage scale:2.0 orientation:UIImageOrientationUp];
UIImage *cutImage = [image1 cropRightAndBottomPixels:2];
self.bubbleImageView.image = [self resizableImage:cutImage];
}];
});
[self.bubbleImageView mas_updateConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.contentLabel).insets(UIEdgeInsetsMake(-10, -10, -10, -10));
}];

View File

@@ -258,7 +258,11 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
for (NSInteger i = 0; i < tempNewDatas.count; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:startIndex + i inSection:0]];
}
[self.messageTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
// 使
[UIView animateWithDuration:0.2 animations:^{
[self.messageTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}];
} else {
[self.messageTableView reloadData];
}
@@ -386,6 +390,23 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
}
}
///
- (BOOL)isGiftMessage:(id)messageData {
if ([messageData isKindOfClass:[NIMMessage class]]) {
NIMMessage *message = (NIMMessage *)messageData;
if ([message.messageObject isKindOfClass:[NIMCustomObject class]]) {
NIMCustomObject *obj = (NIMCustomObject *)message.messageObject;
if (obj.attachment && [obj.attachment isKindOfClass:[AttachmentModel class]]) {
AttachmentModel *attachment = (AttachmentModel *)obj.attachment;
return (attachment.first == CustomMessageType_Gift ||
attachment.first == CustomMessageType_AllMicroSend ||
attachment.first == CustomMessageType_Super_Gift);
}
}
}
return NO;
}
- (NSInteger)getCurrentDataSourceCount {
NSInteger count = 0;
switch (self.displayType) {
@@ -572,7 +593,15 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
self.messageTipsBtn.hidden = NO;
[self findAtMeNumber];
} else {
[self appendAndScrollToBottom];
// 使
if ([self isGiftMessage:messageData]) {
//
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self appendAndScrollToBottom];
});
} else {
[self appendAndScrollToBottom];
}
}
}
@@ -623,22 +652,29 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
if (expectedRows != [self.messageTableView numberOfRowsInSection:0]) {
[self.messageTableView reloadData];
} else {
// indexPath使
NSMutableArray *indexPaths = @[].mutableCopy;
NSInteger startIndex = currentRows;
if (startIndex >= 0 && startIndex <= [self.messageTableView numberOfRowsInSection:0]) {
for (NSInteger i = 0; i < tempArray.count; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:startIndex + i inSection:0]];
}
[self.messageTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
} else {
[self.messageTableView reloadData];
// indexPath使
NSMutableArray *indexPaths = @[].mutableCopy;
NSInteger startIndex = currentRows;
if (startIndex >= 0 && startIndex <= [self.messageTableView numberOfRowsInSection:0]) {
for (NSInteger i = 0; i < tempArray.count; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:startIndex + i inSection:0]];
}
// 使
[UIView animateWithDuration:0.2 animations:^{
[self.messageTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}];
} else {
[self.messageTableView reloadData];
}
}
}
//
[self scrollToBottom:NO];
//
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self scrollToBottom:NO];
});
}
///
@@ -669,7 +705,10 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
[self.messageTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:YES];
});
} else {
[self.messageTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:NO];
// 使
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.messageTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:NO];
} completion:nil];
}
self.atCount = 0;