229 lines
7.5 KiB
Objective-C
229 lines
7.5 KiB
Objective-C
//
|
||
// QXGiftDisplayView.m
|
||
// Test
|
||
//
|
||
// Created by 启星 on 2025/10/20.
|
||
//
|
||
|
||
// QXGiftDisplayView.m
|
||
#import "QXGiftDisplayView.h"
|
||
|
||
@interface QXGiftDisplayView ()
|
||
@property (nonatomic, strong) UIImageView *bgImageView;
|
||
@property (nonatomic, strong) UIImageView *avatarImageView;
|
||
@property (nonatomic, strong) UILabel *senderLabel;
|
||
@property (nonatomic, strong) UILabel *giftLabel;
|
||
@property (nonatomic, strong) UILabel *countLabel;
|
||
@property (nonatomic, strong) UIImageView *giftImageView;
|
||
@property (nonatomic, strong) NSTimer *hideTimer;
|
||
@end
|
||
|
||
@implementation QXGiftDisplayView
|
||
|
||
- (instancetype)initWithFrame:(CGRect)frame {
|
||
self = [super initWithFrame:frame];
|
||
if (self) {
|
||
[self setupUI];
|
||
self.isAnimating = NO;
|
||
}
|
||
return self;
|
||
}
|
||
|
||
- (void)setupUI {
|
||
// self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
|
||
// self.layer.cornerRadius = 20;
|
||
// self.layer.masksToBounds = YES;
|
||
self.bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 223, 40)];
|
||
self.bgImageView.image = [UIImage imageNamed:@"room_gift_display_bg"];
|
||
[self addSubview:self.bgImageView];
|
||
// 发送者头像
|
||
self.avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 32, 32)];
|
||
self.avatarImageView.layer.cornerRadius = 15;
|
||
self.avatarImageView.layer.masksToBounds = YES;
|
||
self.avatarImageView.backgroundColor = [UIColor lightGrayColor];
|
||
self.avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
|
||
[self addSubview:self.avatarImageView];
|
||
|
||
// 发送者名称
|
||
self.senderLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.avatarImageView.right+4, 4, 120, 16)];
|
||
self.senderLabel.font = [UIFont boldSystemFontOfSize:14];
|
||
self.senderLabel.textColor = [UIColor whiteColor];
|
||
[self addSubview:self.senderLabel];
|
||
|
||
// 礼物信息
|
||
self.giftLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.senderLabel.left, self.bgImageView.height-4-14, 120, 14)];
|
||
self.giftLabel.font = [UIFont systemFontOfSize:12];
|
||
self.giftLabel.textColor = RGB16(0xffffff);
|
||
[self addSubview:self.giftLabel];
|
||
|
||
// 礼物图片
|
||
self.giftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bgImageView.width-self.bgImageView.height, 0, self.bgImageView.height, self.bgImageView.height)];
|
||
self.giftImageView.contentMode = UIViewContentModeScaleAspectFit;
|
||
[self addSubview:self.giftImageView];
|
||
|
||
// 礼物数量
|
||
self.countLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.bgImageView.right+8, 5, 100, 35)];
|
||
self.countLabel.font = [UIFont fontWithName:@"DIN Condensed" size:35];
|
||
self.countLabel.textColor = RGB16(0xFFF49F);
|
||
self.countLabel.textAlignment = NSTextAlignmentLeft;
|
||
[self addSubview:self.countLabel];
|
||
|
||
// 初始位置在屏幕左侧外面
|
||
self.frame = CGRectMake(-self.bounds.size.width, self.frame.origin.y, self.bounds.size.width, self.bounds.size.height);
|
||
}
|
||
|
||
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
|
||
UIView *hitView= [super hitTest:point withEvent:event];
|
||
if (hitView== self)
|
||
{
|
||
return nil;
|
||
}
|
||
else
|
||
{
|
||
return hitView;
|
||
}
|
||
}
|
||
// 补全的 updateUIWithGift 方法
|
||
- (void)updateUIWithGift:(QXRoomChatListModel *)gift {
|
||
if (!gift) return;
|
||
|
||
// 更新发送者名称
|
||
self.senderLabel.text = [NSString stringWithFormat:@"%@",gift.FromUserInfo.nickname?: @""];
|
||
|
||
// 更新礼物信息
|
||
self.giftLabel.text = [NSString stringWithFormat:@"%@送给%@",gift.ToUserInfo.nickname?: @"", gift.GiftInfo.gift_name ?: @"礼物"];
|
||
|
||
// 更新礼物数量
|
||
self.countLabel.text = [NSString stringWithFormat:@"x%@", gift.gift_num];
|
||
|
||
// 设置头像(这里可以使用SDWebImage等库加载网络图片)
|
||
[self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:gift.FromUserInfo.avatar]];
|
||
|
||
// 设置礼物图片
|
||
[self.giftImageView sd_setImageWithURL:[NSURL URLWithString:gift.GiftInfo.base_image]];
|
||
}
|
||
|
||
|
||
|
||
// 生成随机颜色(用于测试)
|
||
- (UIColor *)randomColor {
|
||
CGFloat red = arc4random_uniform(255) / 255.0;
|
||
CGFloat green = arc4random_uniform(255) / 255.0;
|
||
CGFloat blue = arc4random_uniform(255) / 255.0;
|
||
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
|
||
}
|
||
|
||
- (void)showGift:(QXRoomChatListModel *)gift {
|
||
// 检查是否已经在动画中
|
||
if (self.isAnimating) {
|
||
NSLog(@"警告: 视图正在动画中,无法显示新礼物");
|
||
return;
|
||
}
|
||
|
||
self.currentGift = gift;
|
||
self.isAnimating = YES;
|
||
|
||
|
||
// 更新UI - 调用补全的方法
|
||
[self updateUIWithGift:gift];
|
||
|
||
// 重置位置和透明度
|
||
self.frame = CGRectMake(-self.bounds.size.width, self.frame.origin.y, self.bounds.size.width, self.bounds.size.height);
|
||
self.alpha = 1.0;
|
||
|
||
// 进入动画
|
||
[UIView animateWithDuration:0.5
|
||
delay:0
|
||
options:UIViewAnimationOptionCurveEaseOut
|
||
animations:^{
|
||
self.frame = CGRectMake(10, self.frame.origin.y, self.bounds.size.width, self.bounds.size.height);
|
||
} completion:^(BOOL finished) {
|
||
if (finished) {
|
||
[self startHideTimer];
|
||
} else {
|
||
// 动画被中断,重置状态
|
||
self.isAnimating = NO;
|
||
self.currentGift = nil;
|
||
}
|
||
}];
|
||
}
|
||
|
||
- (void)updateGiftCount:(NSInteger)count {
|
||
if (!self.isAnimating) {
|
||
NSLog(@"警告: 视图不在动画中,无法更新数量");
|
||
return;
|
||
}
|
||
|
||
NSLog(@"更新礼物数量: %ld", (long)count);
|
||
|
||
// 更新数量显示
|
||
self.countLabel.text = [NSString stringWithFormat:@"x%ld", (long)count];
|
||
|
||
// 数量更新动画
|
||
[UIView animateWithDuration:0.2 animations:^{
|
||
self.countLabel.transform = CGAffineTransformMakeScale(1.5, 1.5);
|
||
} completion:^(BOOL finished) {
|
||
[UIView animateWithDuration:0.2 animations:^{
|
||
self.countLabel.transform = CGAffineTransformIdentity;
|
||
}];
|
||
|
||
// 重置计时器
|
||
[self resetHideTimer];
|
||
}];
|
||
}
|
||
|
||
- (void)startHideTimer {
|
||
[self.hideTimer invalidate];
|
||
self.hideTimer = [NSTimer scheduledTimerWithTimeInterval:3.0
|
||
target:self
|
||
selector:@selector(hideAnimation)
|
||
userInfo:nil
|
||
repeats:NO];
|
||
}
|
||
|
||
- (void)resetHideTimer {
|
||
[self.hideTimer invalidate];
|
||
[self startHideTimer];
|
||
}
|
||
|
||
- (void)hideAnimation {
|
||
if (!self.isAnimating) {
|
||
return;
|
||
}
|
||
|
||
[UIView animateWithDuration:0.5
|
||
animations:^{
|
||
self.frame = CGRectMake(-self.bounds.size.width, self.frame.origin.y, self.bounds.size.width, self.bounds.size.height);
|
||
self.alpha = 0.0;
|
||
} completion:^(BOOL finished) {
|
||
self.isAnimating = NO;
|
||
|
||
if ([self.delegate respondsToSelector:@selector(QXGiftDisplayViewDidFinishAnimation:)]) {
|
||
[self.delegate QXGiftDisplayViewDidFinishAnimation:self];
|
||
}
|
||
|
||
self.currentGift = nil;
|
||
}];
|
||
}
|
||
|
||
- (void)finishAnimationImmediately {
|
||
NSLog(@"立即结束动画");
|
||
|
||
[self.hideTimer invalidate];
|
||
self.hideTimer = nil;
|
||
|
||
[self.layer removeAllAnimations];
|
||
self.isAnimating = NO;
|
||
self.currentGift = nil;
|
||
self.alpha = 0.0;
|
||
self.frame = CGRectMake(-self.bounds.size.width, self.frame.origin.y, self.bounds.size.width, self.bounds.size.height);
|
||
}
|
||
|
||
- (void)dealloc {
|
||
[self.hideTimer invalidate];
|
||
self.hideTimer = nil;
|
||
NSLog(@"QXGiftDisplayView dealloc");
|
||
}
|
||
|
||
@end
|